Skip to content

Feature Request: Add an option to display only errors in cargo check #15407

Open
@SuZenith

Description

@SuZenith

Problem

Summary

In collaborative projects, developers often encounter warnings (warnings) generated by code written by other team members. While warnings are important for improving code quality, they may not always be the immediate focus of the developer. Errors (errors), on the other hand, are critical and need to be addressed promptly as they block compilation. Currently, cargo check outputs both warnings and errors without an option to filter them.

This feature request proposes adding an option to cargo check to display only errors, making it easier for developers to focus on critical issues while leaving warnings for later or for other team members to address.

Motivation

In large-scale projects, especially those involving multiple contributors, warnings can accumulate due to different coding styles, incomplete implementations, or temporary issues. However, fixing warnings is often not the immediate priority for a developer working on a specific task. Errors, which prevent code from compiling, are more urgent and need immediate attention.

A typical scenario:

  • A developer runs cargo check to verify their changes.
  • The output contains both warnings and errors, making it harder to quickly identify the errors that need fixing.
  • Filtering out warnings manually (e.g., using grep) is possible but cumbersome, especially for less experienced developers or in CI pipelines.
  • Adding a built-in option to cargo check to display only errors would streamline the workflow and improve developer productivity.

Proposed Solution

Introduce a new command-line flag for cargo check, such as:

--only-errors: When specified, cargo check will suppress warnings and display only errors.
Alternatively:

--filter=errors: A more generic filtering mechanism that allows selecting specific message types (e.g., errors, warnings, or both).

Design Details

  • The flag should suppress warnings from both the compiler and Cargo itself, ensuring a clean output with only error messages.
  • The implementation could leverage the existing --message-format=json option internally, filtering messages based on their level field (error, warning, etc.).
  • Example usage:
cargo check --only-errors

Expected output:

error[E0425]: cannot find value `x` in this scope
  --> src/main.rs:3:9

Notes

Alternatives

While this functionality can currently be achieved using external tools like grep or jq, these methods require additional setup and are not intuitive for developers unfamiliar with shell scripting or JSON processing. Examples:

  • Using grep:
cargo check 2>&1 | grep "error:"
  • Using jq:
cargo check --message-format=json | jq 'select(.reason == "compiler-message" and .message.level == "error") | .message.rendered' -r

These approaches are functional but lack the simplicity and integration of a built-in Cargo feature.

Impact

  • Developers can focus on fixing errors without being distracted by warnings.
  • CI pipelines can use this flag to focus on blocking issues during specific stages of development.
  • Improves usability for new developers who may find filtering messages manually cumbersome.

Additional Context

This feature aligns with the philosophy of making Rust tooling ergonomic and developer-friendly. Similar filtering mechanisms exist in other build systems and compilers, making this a familiar and expected feature for developers transitioning to Rust.

I hope this feature can be considered for inclusion in Cargo. Thank you for your time and efforts in maintaining and improving the Rust ecosystem!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Error and warning messages generated by Cargo itself.C-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`S-needs-infoStatus: Needs more info, such as a reproduction or more background for a feature request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions