Skip to content

make path_statements lint machine applicable for statements with no effect #140830

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ lint_path_statement_drop = path statement drops value
.suggestion = use `drop` to clarify the intent

lint_path_statement_no_effect = path statement with no effect
.suggestion = remove this statement

lint_pattern_in_bodiless = patterns aren't allowed in functions without bodies
.label = pattern not allowed in function without body
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,10 @@ pub(crate) enum PathStatementDropSub {

#[derive(LintDiagnostic)]
#[diag(lint_path_statement_no_effect)]
pub(crate) struct PathStatementNoEffect;
pub(crate) struct PathStatementNoEffect {
#[suggestion(lint_suggestion, code = "", applicability = "machine-applicable")]
pub suggestion: Span,
}

#[derive(LintDiagnostic)]
#[diag(lint_unused_delim)]
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,11 @@ impl<'tcx> LateLintPass<'tcx> for PathStatements {
};
cx.emit_span_lint(PATH_STATEMENTS, s.span, PathStatementDrop { sub })
} else {
cx.emit_span_lint(PATH_STATEMENTS, s.span, PathStatementNoEffect);
cx.emit_span_lint(
PATH_STATEMENTS,
s.span,
PathStatementNoEffect { suggestion: s.span },
);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/lint/warn-path-statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ fn main() {

let z = (Droppy,);
z; //~ ERROR path statement drops value

macro_rules! foo {
($e:expr) => {
$e;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I think you'll need a

//~^ ERROR: path statement with no effect

annotation here. I guess partly what I had in mind re. macros was also this case, where the suggestion is tied to removing $e;, though arguably maybe it's the invocation foo!(x); that should be removed.

Again, this is not blocking IMO.

};
}

foo!(x);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jieyouxu is this what you meant? Right now this is emitting the lint. Is the idea that this lint shouldn't be emitted the lint in this case or that it shouldn't be machine applicable?

Copy link
Member

@jieyouxu jieyouxu Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I somehow completely lost this notification, sorry about that. Now that I read my own comment #140830 (review) back, I think what I had in mind at the time was stuff like having a path statement that comes from a macro expansion from another crate, where I think I thought that a machine-applicable suggestion in that case wouldn't be very actionable.

... But that can be a follow-up or further adjusted as suitable, and does not need to block this PR at all.

}
15 changes: 13 additions & 2 deletions tests/ui/lint/warn-path-statement.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: path statement with no effect
--> $DIR/warn-path-statement.rs:10:5
|
LL | x;
| ^^
| ^^ help: remove this statement
|
= note: requested on the command line with `-D path-statements`

Expand All @@ -18,5 +18,16 @@ error: path statement drops value
LL | z;
| ^^ help: use `drop` to clarify the intent: `drop(z);`

error: aborting due to 3 previous errors
error: path statement with no effect
--> $DIR/warn-path-statement.rs:20:13
|
LL | $e;
| ^^^ help: remove this statement
...
LL | foo!(x);
| ------- in this macro invocation
|
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 4 previous errors

Loading