diff --git a/CHANGELOG.md b/CHANGELOG.md index d0f511742fa1..3c311879cb17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index a4948e36fbe2..dfb6367bfeaa 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -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, diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 3328d642bd85..a55a6d701213 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -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; @@ -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` } diff --git a/clippy_lints/src/wildcard_let.rs b/clippy_lints/src/wildcard_let.rs new file mode 100644 index 000000000000..a0ef462aa983 --- /dev/null +++ b/clippy_lints/src/wildcard_let.rs @@ -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", + ); + } + } +} diff --git a/tests/ui/wildcard_let.rs b/tests/ui/wildcard_let.rs new file mode 100644 index 000000000000..eb0ed5134585 --- /dev/null +++ b/tests/ui/wildcard_let.rs @@ -0,0 +1,5 @@ +#![forbid(clippy::wildcard_let)] + +fn main() { + let _ = 1; +} diff --git a/tests/ui/wildcard_let.stderr b/tests/ui/wildcard_let.stderr new file mode 100644 index 000000000000..0c76d15e7484 --- /dev/null +++ b/tests/ui/wildcard_let.stderr @@ -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 +