-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add wildcard_let lint #12902
Closed
Closed
Add wildcard_let lint #12902
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
23bd7ac
Add wildcard_let lint
lolbinarycat ad2f078
fix compiler warnings in wildcard_let.rs
lolbinarycat 6cb4285
add ui test for wildcard_let lint
lolbinarycat 1b6a1f0
autoformat wildcard_let
lolbinarycat 537174c
add wildcard_let link to changelog
lolbinarycat 6741541
add clippy::version for wildcard_let
lolbinarycat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use rustc_ast::ast::{Local, PatKind}; | ||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::impl_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// check for `let _ = ...`. | ||
/// | ||
/// this may be used by crates that with to force `#[must_use]` | ||
/// values to actually used, along with `#[forbid(unused_must_use)]`. | ||
#[clippy::version = "1.80.0"] | ||
pub WILDCARD_LET, | ||
restriction, | ||
"wildcard let" | ||
} | ||
impl_lint_pass!(WildcardLet => [WILDCARD_LET]); | ||
|
||
pub struct WildcardLet {} | ||
|
||
impl EarlyLintPass for WildcardLet { | ||
fn check_local(&mut self, cx: &EarlyContext<'_>, local: &Local) { | ||
let span = local.pat.span; | ||
if in_external_macro(cx.sess(), span) { | ||
return; | ||
} | ||
if let PatKind::Wild = local.pat.kind { | ||
span_lint_and_help( | ||
cx, | ||
WILDCARD_LET, | ||
span, | ||
"wildcard let", | ||
None, | ||
"remove this binding or handle the value", | ||
); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#![forbid(clippy::wildcard_let)] | ||
|
||
fn main() { | ||
let _ = 1; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error: wildcard let | ||
--> tests/ui/wildcard_let.rs:4:9 | ||
| | ||
LL | let _ = 1; | ||
| ^ | ||
| | ||
= help: remove this binding or handle the value | ||
note: the lint level is defined here | ||
--> tests/ui/wildcard_let.rs:1:11 | ||
| | ||
LL | #![forbid(clippy::wildcard_let)] | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 1 previous error | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this use-case not already covered by
let_underscore_must_use
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the 5 years of #4090 being open, noone brought that lint up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I understand there is some overlap, but the wildcard_let lint is broader in scope, correct? In that case, the lint should at least check if the
let_underscore_must_use
lint would apply and avoid linting in that case to avoid double messages.