Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
SuZenith opened this issue Apr 8, 2025 · 1 comment
Open
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request.

Comments

@SuZenith
Copy link

SuZenith commented Apr 8, 2025

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!

@SuZenith SuZenith added C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage. labels Apr 8, 2025
@weihanglo
Copy link
Member

Thanks for the well-written proposal!

Have you checked out the unstable build.warnings config option? I feel like it should cover the use case you just described!

See also

@weihanglo weihanglo added A-diagnostics Area: Error and warning messages generated by Cargo itself. S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request. and removed S-triage Status: This issue is waiting on initial triage. labels Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request.
Projects
None yet
Development

No branches or pull requests

2 participants