Skip to content

Commit

Permalink
Auto merge of rust-lang#10467 - blyxyas:underscore_typed, r=Jarcho
Browse files Browse the repository at this point in the history
Add `let_with_type_underscore` lint

Fixes rust-lang#10463
changelog: [`let_with_type_underscore`]: Add the lint.
  • Loading branch information
bors committed Mar 9, 2023
2 parents ea4ebed + ca3bf94 commit 5f98734
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 65 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4643,6 +4643,7 @@ Released 2018-09-13
[`let_underscore_must_use`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_must_use
[`let_underscore_untyped`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_untyped
[`let_unit_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value
[`let_with_type_underscore`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_with_type_underscore
[`linkedlist`]: https://rust-lang.github.io/rust-clippy/master/index.html#linkedlist
[`logic_bug`]: https://rust-lang.github.io/rust-clippy/master/index.html#logic_bug
[`lossy_float_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#lossy_float_literal
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 @@ -227,6 +227,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::let_underscore::LET_UNDERSCORE_LOCK_INFO,
crate::let_underscore::LET_UNDERSCORE_MUST_USE_INFO,
crate::let_underscore::LET_UNDERSCORE_UNTYPED_INFO,
crate::let_with_type_underscore::LET_WITH_TYPE_UNDERSCORE_INFO,
crate::lifetimes::EXTRA_UNUSED_LIFETIMES_INFO,
crate::lifetimes::NEEDLESS_LIFETIMES_INFO,
crate::literal_representation::DECIMAL_LITERAL_REPRESENTATION_INFO,
Expand Down
45 changes: 45 additions & 0 deletions clippy_lints/src/let_with_type_underscore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use clippy_utils::diagnostics::span_lint_and_help;
use rustc_hir::*;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint};

declare_clippy_lint! {
/// ### What it does
/// Detects when a variable is declared with an explicit type of `_`.
/// ### Why is this bad?
/// It adds noise, `: _` provides zero clarity or utility.
/// ### Example
/// ```rust,ignore
/// let my_number: _ = 1;
/// ```
/// Use instead:
/// ```rust,ignore
/// let my_number = 1;
/// ```
#[clippy::version = "1.69.0"]
pub LET_WITH_TYPE_UNDERSCORE,
complexity,
"unneeded underscore type (`_`) in a variable declaration"
}
declare_lint_pass!(UnderscoreTyped => [LET_WITH_TYPE_UNDERSCORE]);

impl LateLintPass<'_> for UnderscoreTyped {
fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
if_chain! {
if !in_external_macro(cx.tcx.sess, local.span);
if let Some(ty) = local.ty; // Ensure that it has a type defined
if let TyKind::Infer = &ty.kind; // that type is '_'
if local.span.ctxt() == ty.span.ctxt();
then {
span_lint_and_help(cx,
LET_WITH_TYPE_UNDERSCORE,
local.span,
"variable declared with type underscore",
Some(ty.span.with_lo(local.pat.span.hi())),
"remove the explicit type `_` declaration"
)
}
};
}
}
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ mod large_stack_arrays;
mod len_zero;
mod let_if_seq;
mod let_underscore;
mod let_with_type_underscore;
mod lifetimes;
mod literal_representation;
mod loops;
Expand Down Expand Up @@ -930,6 +931,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|_| Box::new(collection_is_never_read::CollectionIsNeverRead));
store.register_late_pass(|_| Box::new(missing_assert_message::MissingAssertMessage));
store.register_early_pass(|| Box::new(redundant_async_block::RedundantAsyncBlock));
store.register_late_pass(|_| Box::new(let_with_type_underscore::UnderscoreTyped));
// add lints here, do not remove this comment, it's used in `new_lint`
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/crashes/ice-6179.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! The ICE is mainly caused by using `hir_ty_to_ty`. See the discussion in the PR for details.
#![warn(clippy::use_self)]
#![allow(dead_code)]
#![allow(dead_code, clippy::let_with_type_underscore)]

struct Foo;

Expand Down
3 changes: 2 additions & 1 deletion tests/ui/default_numeric_fallback_f64.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
clippy::unnecessary_operation,
clippy::branches_sharing_code,
clippy::match_single_binding,
clippy::let_unit_value
clippy::let_unit_value,
clippy::let_with_type_underscore
)]

#[macro_use]
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/default_numeric_fallback_f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
clippy::unnecessary_operation,
clippy::branches_sharing_code,
clippy::match_single_binding,
clippy::let_unit_value
clippy::let_unit_value,
clippy::let_with_type_underscore
)]

#[macro_use]
Expand Down
48 changes: 24 additions & 24 deletions tests/ui/default_numeric_fallback_f64.stderr
Original file line number Diff line number Diff line change
@@ -1,145 +1,145 @@
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:21:17
--> $DIR/default_numeric_fallback_f64.rs:22:17
|
LL | let x = 0.12;
| ^^^^ help: consider adding suffix: `0.12_f64`
|
= note: `-D clippy::default-numeric-fallback` implied by `-D warnings`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:22:18
--> $DIR/default_numeric_fallback_f64.rs:23:18
|
LL | let x = [1., 2., 3.];
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:22:22
--> $DIR/default_numeric_fallback_f64.rs:23:22
|
LL | let x = [1., 2., 3.];
| ^^ help: consider adding suffix: `2.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:22:26
--> $DIR/default_numeric_fallback_f64.rs:23:26
|
LL | let x = [1., 2., 3.];
| ^^ help: consider adding suffix: `3.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:23:28
--> $DIR/default_numeric_fallback_f64.rs:24:28
|
LL | let x = if true { (1., 2.) } else { (3., 4.) };
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:23:32
--> $DIR/default_numeric_fallback_f64.rs:24:32
|
LL | let x = if true { (1., 2.) } else { (3., 4.) };
| ^^ help: consider adding suffix: `2.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:23:46
--> $DIR/default_numeric_fallback_f64.rs:24:46
|
LL | let x = if true { (1., 2.) } else { (3., 4.) };
| ^^ help: consider adding suffix: `3.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:23:50
--> $DIR/default_numeric_fallback_f64.rs:24:50
|
LL | let x = if true { (1., 2.) } else { (3., 4.) };
| ^^ help: consider adding suffix: `4.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:24:23
--> $DIR/default_numeric_fallback_f64.rs:25:23
|
LL | let x = match 1. {
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:25:18
--> $DIR/default_numeric_fallback_f64.rs:26:18
|
LL | _ => 1.,
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:44:21
--> $DIR/default_numeric_fallback_f64.rs:45:21
|
LL | let y = 1.;
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:52:21
--> $DIR/default_numeric_fallback_f64.rs:53:21
|
LL | let y = 1.;
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:58:21
--> $DIR/default_numeric_fallback_f64.rs:59:21
|
LL | let y = 1.;
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:66:21
--> $DIR/default_numeric_fallback_f64.rs:67:21
|
LL | let y = 1.;
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:78:9
--> $DIR/default_numeric_fallback_f64.rs:79:9
|
LL | 1.
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:84:27
--> $DIR/default_numeric_fallback_f64.rs:85:27
|
LL | let f = || -> _ { 1. };
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:88:29
--> $DIR/default_numeric_fallback_f64.rs:89:29
|
LL | let f = || -> f64 { 1. };
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:102:21
--> $DIR/default_numeric_fallback_f64.rs:103:21
|
LL | generic_arg(1.);
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:105:32
--> $DIR/default_numeric_fallback_f64.rs:106:32
|
LL | let x: _ = generic_arg(1.);
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:123:28
--> $DIR/default_numeric_fallback_f64.rs:124:28
|
LL | GenericStruct { x: 1. };
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:126:36
--> $DIR/default_numeric_fallback_f64.rs:127:36
|
LL | let _ = GenericStruct { x: 1. };
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:144:24
--> $DIR/default_numeric_fallback_f64.rs:145:24
|
LL | GenericEnum::X(1.);
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:164:23
--> $DIR/default_numeric_fallback_f64.rs:165:23
|
LL | s.generic_arg(1.);
| ^^ help: consider adding suffix: `1.0_f64`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:171:21
--> $DIR/default_numeric_fallback_f64.rs:172:21
|
LL | let x = 22.;
| ^^^ help: consider adding suffix: `22.0_f64`
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/default_numeric_fallback_i32.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
clippy::no_effect,
clippy::unnecessary_operation,
clippy::branches_sharing_code,
clippy::let_unit_value
clippy::let_unit_value,
clippy::let_with_type_underscore
)]

#[macro_use]
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/default_numeric_fallback_i32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
clippy::no_effect,
clippy::unnecessary_operation,
clippy::branches_sharing_code,
clippy::let_unit_value
clippy::let_unit_value,
clippy::let_with_type_underscore
)]

#[macro_use]
Expand Down
Loading

0 comments on commit 5f98734

Please sign in to comment.