Skip to content
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
wants to merge 6 commits into from
Closed
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5920,6 +5920,7 @@ Released 2018-09-13
[`wildcard_enum_match_arm`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_enum_match_arm
[`wildcard_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_imports
[`wildcard_in_or_patterns`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_in_or_patterns
[`wildcard_let`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_let
[`write_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#write_literal
[`write_with_newline`]: https://rust-lang.github.io/rust-clippy/master/index.html#write_with_newline
[`writeln_empty_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#writeln_empty_string
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::visibility::PUB_WITH_SHORTHAND_INFO,
crate::wildcard_imports::ENUM_GLOB_USE_INFO,
crate::wildcard_imports::WILDCARD_IMPORTS_INFO,
crate::wildcard_let::WILDCARD_LET_INFO,
crate::write::PRINTLN_EMPTY_STRING_INFO,
crate::write::PRINT_LITERAL_INFO,
crate::write::PRINT_STDERR_INFO,
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ mod vec;
mod vec_init_then_push;
mod visibility;
mod wildcard_imports;
mod wildcard_let;
mod write;
mod zero_div_zero;
mod zero_repeat_side_effects;
Expand Down Expand Up @@ -1165,6 +1166,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
..Default::default()
})
});
store.register_early_pass(|| Box::new(wildcard_let::WildcardLet {}));
// add lints here, do not remove this comment, it's used in `new_lint`
}

Expand Down
39 changes: 39 additions & 0 deletions clippy_lints/src/wildcard_let.rs
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)]`.
Comment on lines +11 to +12
Copy link
Member

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?

Copy link
Author

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.

Copy link
Contributor

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.

#[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",
);
}
}
}
5 changes: 5 additions & 0 deletions tests/ui/wildcard_let.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![forbid(clippy::wildcard_let)]

fn main() {
let _ = 1;
}
15 changes: 15 additions & 0 deletions tests/ui/wildcard_let.stderr
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

Loading