Skip to content

Commit 5f98734

Browse files
committed
Auto merge of rust-lang#10467 - blyxyas:underscore_typed, r=Jarcho
Add `let_with_type_underscore` lint Fixes rust-lang#10463 changelog: [`let_with_type_underscore`]: Add the lint.
2 parents ea4ebed + ca3bf94 commit 5f98734

16 files changed

+178
-65
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4643,6 +4643,7 @@ Released 2018-09-13
46434643
[`let_underscore_must_use`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_must_use
46444644
[`let_underscore_untyped`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_untyped
46454645
[`let_unit_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value
4646+
[`let_with_type_underscore`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_with_type_underscore
46464647
[`linkedlist`]: https://rust-lang.github.io/rust-clippy/master/index.html#linkedlist
46474648
[`logic_bug`]: https://rust-lang.github.io/rust-clippy/master/index.html#logic_bug
46484649
[`lossy_float_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#lossy_float_literal

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
227227
crate::let_underscore::LET_UNDERSCORE_LOCK_INFO,
228228
crate::let_underscore::LET_UNDERSCORE_MUST_USE_INFO,
229229
crate::let_underscore::LET_UNDERSCORE_UNTYPED_INFO,
230+
crate::let_with_type_underscore::LET_WITH_TYPE_UNDERSCORE_INFO,
230231
crate::lifetimes::EXTRA_UNUSED_LIFETIMES_INFO,
231232
crate::lifetimes::NEEDLESS_LIFETIMES_INFO,
232233
crate::literal_representation::DECIMAL_LITERAL_REPRESENTATION_INFO,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use rustc_hir::*;
3+
use rustc_lint::{LateContext, LateLintPass};
4+
use rustc_middle::lint::in_external_macro;
5+
use rustc_session::{declare_lint_pass, declare_tool_lint};
6+
7+
declare_clippy_lint! {
8+
/// ### What it does
9+
/// Detects when a variable is declared with an explicit type of `_`.
10+
/// ### Why is this bad?
11+
/// It adds noise, `: _` provides zero clarity or utility.
12+
/// ### Example
13+
/// ```rust,ignore
14+
/// let my_number: _ = 1;
15+
/// ```
16+
/// Use instead:
17+
/// ```rust,ignore
18+
/// let my_number = 1;
19+
/// ```
20+
#[clippy::version = "1.69.0"]
21+
pub LET_WITH_TYPE_UNDERSCORE,
22+
complexity,
23+
"unneeded underscore type (`_`) in a variable declaration"
24+
}
25+
declare_lint_pass!(UnderscoreTyped => [LET_WITH_TYPE_UNDERSCORE]);
26+
27+
impl LateLintPass<'_> for UnderscoreTyped {
28+
fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
29+
if_chain! {
30+
if !in_external_macro(cx.tcx.sess, local.span);
31+
if let Some(ty) = local.ty; // Ensure that it has a type defined
32+
if let TyKind::Infer = &ty.kind; // that type is '_'
33+
if local.span.ctxt() == ty.span.ctxt();
34+
then {
35+
span_lint_and_help(cx,
36+
LET_WITH_TYPE_UNDERSCORE,
37+
local.span,
38+
"variable declared with type underscore",
39+
Some(ty.span.with_lo(local.pat.span.hi())),
40+
"remove the explicit type `_` declaration"
41+
)
42+
}
43+
};
44+
}
45+
}

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ mod large_stack_arrays;
167167
mod len_zero;
168168
mod let_if_seq;
169169
mod let_underscore;
170+
mod let_with_type_underscore;
170171
mod lifetimes;
171172
mod literal_representation;
172173
mod loops;
@@ -930,6 +931,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
930931
store.register_late_pass(|_| Box::new(collection_is_never_read::CollectionIsNeverRead));
931932
store.register_late_pass(|_| Box::new(missing_assert_message::MissingAssertMessage));
932933
store.register_early_pass(|| Box::new(redundant_async_block::RedundantAsyncBlock));
934+
store.register_late_pass(|_| Box::new(let_with_type_underscore::UnderscoreTyped));
933935
// add lints here, do not remove this comment, it's used in `new_lint`
934936
}
935937

tests/ui/crashes/ice-6179.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! The ICE is mainly caused by using `hir_ty_to_ty`. See the discussion in the PR for details.
33
44
#![warn(clippy::use_self)]
5-
#![allow(dead_code)]
5+
#![allow(dead_code, clippy::let_with_type_underscore)]
66

77
struct Foo;
88

tests/ui/default_numeric_fallback_f64.fixed

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
clippy::unnecessary_operation,
1010
clippy::branches_sharing_code,
1111
clippy::match_single_binding,
12-
clippy::let_unit_value
12+
clippy::let_unit_value,
13+
clippy::let_with_type_underscore
1314
)]
1415

1516
#[macro_use]

tests/ui/default_numeric_fallback_f64.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
clippy::unnecessary_operation,
1010
clippy::branches_sharing_code,
1111
clippy::match_single_binding,
12-
clippy::let_unit_value
12+
clippy::let_unit_value,
13+
clippy::let_with_type_underscore
1314
)]
1415

1516
#[macro_use]

tests/ui/default_numeric_fallback_f64.stderr

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,145 @@
11
error: default numeric fallback might occur
2-
--> $DIR/default_numeric_fallback_f64.rs:21:17
2+
--> $DIR/default_numeric_fallback_f64.rs:22:17
33
|
44
LL | let x = 0.12;
55
| ^^^^ help: consider adding suffix: `0.12_f64`
66
|
77
= note: `-D clippy::default-numeric-fallback` implied by `-D warnings`
88

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

141141
error: default numeric fallback might occur
142-
--> $DIR/default_numeric_fallback_f64.rs:171:21
142+
--> $DIR/default_numeric_fallback_f64.rs:172:21
143143
|
144144
LL | let x = 22.;
145145
| ^^^ help: consider adding suffix: `22.0_f64`

tests/ui/default_numeric_fallback_i32.fixed

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
clippy::no_effect,
1010
clippy::unnecessary_operation,
1111
clippy::branches_sharing_code,
12-
clippy::let_unit_value
12+
clippy::let_unit_value,
13+
clippy::let_with_type_underscore
1314
)]
1415

1516
#[macro_use]

tests/ui/default_numeric_fallback_i32.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
clippy::no_effect,
1010
clippy::unnecessary_operation,
1111
clippy::branches_sharing_code,
12-
clippy::let_unit_value
12+
clippy::let_unit_value,
13+
clippy::let_with_type_underscore
1314
)]
1415

1516
#[macro_use]

0 commit comments

Comments
 (0)