hir-ty: emit diagnostic for unused #[must_use] values#22239
Merged
ChayimFriedman2 merged 1 commit intoMay 2, 2026
Conversation
ChayimFriedman2
requested changes
May 1, 2026
| "unstable" => attr_flags.insert(AttrFlags::IS_UNSTABLE), | ||
| "deprecated" => attr_flags.insert(AttrFlags::IS_DEPRECATED), | ||
| "macro_export" => attr_flags.insert(AttrFlags::IS_MACRO_EXPORT), | ||
| "must_use" => attr_flags.insert(AttrFlags::IS_MUST_USE), |
Contributor
There was a problem hiding this comment.
A possible enhancement (not necessarily in this PR) is to also have a query to fetch the must_use reason, like other queries in this file.
Comment on lines
+333
to
+339
| if self.validate_lints { | ||
| for stmt in &**statements { | ||
| if let &Statement::Expr { expr: stmt_expr, has_semi: true } = stmt { | ||
| self.check_unused_must_use(stmt_expr); | ||
| } | ||
| } | ||
| } |
Contributor
There was a problem hiding this comment.
Instead of looping over the expressions twice, please combine them with the run below in this function. It might make sense to extract the existing handling to a function.
MavenRain
added a commit
to MavenRain/rust-analyzer
that referenced
this pull request
May 1, 2026
Address review feedback on rust-lang#22239: combine the must-use loop and the non-exhaustive-let loop into a single pass over `statements`, and lift the existing handling into a dedicated `check_non_exhaustive_let` method. `check_unused_must_use` and `check_non_exhaustive_let` now return `Option<BodyValidationDiagnostic>`; the caller pushes the result. This keeps `&self.infcx` (held by `MatchCheckCtx`) compatible with the mutable borrow on `self.diagnostics` via field-level split borrows.
d91195b to
62c49de
Compare
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
Addressed the expression looping . . . let me know if adequate |
ChayimFriedman2
requested changes
May 1, 2026
MavenRain
added a commit
to MavenRain/rust-analyzer
that referenced
this pull request
May 1, 2026
Address review feedback on rust-lang#22239: combine the must-use loop and the non-exhaustive-let loop into a single pass over `statements`, and lift the existing handling into a dedicated `check_non_exhaustive_let` method. `check_unused_must_use` and `check_non_exhaustive_let` now return `Option<BodyValidationDiagnostic>`; the caller pushes the result. This keeps `&self.infcx` (held by `MatchCheckCtx`) compatible with the mutable borrow on `self.diagnostics` via field-level split borrows.
62c49de to
27d5f30
Compare
This comment has been minimized.
This comment has been minimized.
Address review feedback on rust-lang#22239: combine the must-use loop and the non-exhaustive-let loop into a single pass over `statements`, and lift the existing handling into a dedicated `check_non_exhaustive_let` method. `check_unused_must_use` and `check_non_exhaustive_let` now return `Option<BodyValidationDiagnostic>`; the caller pushes the result. This keeps `&self.infcx` (held by `MatchCheckCtx`) compatible with the mutable borrow on `self.diagnostics` via field-level split borrows.
27d5f30 to
9f186ad
Compare
ChayimFriedman2
approved these changes
May 2, 2026
lnicola
pushed a commit
to lnicola/rust
that referenced
this pull request
May 4, 2026
Address review feedback on rust-lang/rust-analyzer#22239: combine the must-use loop and the non-exhaustive-let loop into a single pass over `statements`, and lift the existing handling into a dedicated `check_non_exhaustive_let` method. `check_unused_must_use` and `check_non_exhaustive_let` now return `Option<BodyValidationDiagnostic>`; the caller pushes the result. This keeps `&self.infcx` (held by `MatchCheckCtx`) compatible with the mutable borrow on `self.diagnostics` via field-level split borrows.
MavenRain
added a commit
to MavenRain/rust-analyzer
that referenced
this pull request
Jun 7, 2026
ExprValidator::check_unused_must_use previously matched only Expr::Call and Expr::MethodCall directly on the expression of a Statement::Expr with semicolon. This left several stmt-with-semi cases unwarned: blocks whose tail is a #[must_use] call, unsafe-blocks, if/else arms, match arms, and const blocks.
Refactors check_unused_must_use to walk into Expr::Block { tail }, Expr::Unsafe { tail }, Expr::If { then_branch, else_branch }, Expr::Match { arms }, and Expr::Const(inner) before applying the existing leaf check (callee or method #[must_use] attribute, or #[must_use] ADT return type). The diagnostic stays at the leaf call so the span points to the actual must_use producer rather than the wrapping block.
Adds ide-diagnostics tests covering each new container case.
Follow-up to rust-lang#22239.
Signed-off-by: Onyeka Obi <softwareengineerasaservant@isurvivable.cv>
lnicola
pushed a commit
to lnicola/rust
that referenced
this pull request
Jul 13, 2026
ExprValidator::check_unused_must_use previously matched only Expr::Call and Expr::MethodCall directly on the expression of a Statement::Expr with semicolon. This left several stmt-with-semi cases unwarned: blocks whose tail is a #[must_use] call, unsafe-blocks, if/else arms, match arms, and const blocks.
Refactors check_unused_must_use to walk into Expr::Block { tail }, Expr::Unsafe { tail }, Expr::If { then_branch, else_branch }, Expr::Match { arms }, and Expr::Const(inner) before applying the existing leaf check (callee or method #[must_use] attribute, or #[must_use] ADT return type). The diagnostic stays at the leaf call so the span points to the actual must_use producer rather than the wrapping block.
Adds ide-diagnostics tests covering each new container case.
Follow-up to rust-lang/rust-analyzer#22239.
Signed-off-by: Onyeka Obi <softwareengineerasaservant@isurvivable.cv>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds the unused_must_use lint that warns when the result of a call or method call is dropped despite the callee or its return type carrying #[must_use].
must_usesymbol.#[must_use](PathMeta) and#[must_use = "reason"](KeyValueMeta).