Skip to content

Commit 9b0c1eb

Browse files
committed
Change to span_lint_and_sugg from span_lint_and_help
1 parent 1f8ee3c commit 9b0c1eb

File tree

2 files changed

+38
-76
lines changed

2 files changed

+38
-76
lines changed

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_ast::ast::{LitFloatType, LitIntType, LitKind};
2+
use rustc_errors::Applicability;
23
use rustc_hir::{
34
intravisit::{walk_expr, walk_stmt, NestedVisitorMap, Visitor},
45
Body, Expr, ExprKind, HirId, Lit, Stmt, StmtKind,
@@ -12,7 +13,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
1213

1314
use if_chain::if_chain;
1415

15-
use crate::utils::span_lint_and_help;
16+
use crate::utils::{snippet, span_lint_and_sugg};
1617

1718
declare_clippy_lint! {
1819
/// **What it does:** Checks for usage of unconstrained numeric literals which may cause default numeric fallback in type
@@ -75,16 +76,24 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
7576
if let Some(ty_bound) = self.ty_bounds.last();
7677
if matches!(lit.node,
7778
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed));
78-
if matches!(lit_ty.kind(), ty::Int(IntTy::I32) | ty::Float(FloatTy::F64));
7979
if !ty_bound.is_integral();
8080
then {
81-
span_lint_and_help(
81+
let suffix = match lit_ty.kind() {
82+
ty::Int(IntTy::I32) => "i32",
83+
ty::Float(FloatTy::F64) => "f64",
84+
// Default numeric fallback never results in other types.
85+
_ => return,
86+
};
87+
88+
let sugg = format!("{}_{}", snippet(self.cx, lit.span, ""), suffix);
89+
span_lint_and_sugg(
8290
self.cx,
8391
DEFAULT_NUMERIC_FALLBACK,
8492
lit.span,
8593
"default numeric fallback might occur",
86-
None,
87-
"consider adding suffix to avoid default numeric fallback",
94+
"consider adding suffix",
95+
sugg,
96+
Applicability::MaybeIncorrect,
8897
);
8998
}
9099
}

tests/ui/default_numeric_fallback.stderr

Lines changed: 24 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,194 +2,147 @@ error: default numeric fallback might occur
22
--> $DIR/default_numeric_fallback.rs:10:17
33
|
44
LL | let x = 22;
5-
| ^^
5+
| ^^ help: consider adding suffix: `22_i32`
66
|
77
= note: `-D clippy::default-numeric-fallback` implied by `-D warnings`
8-
= help: consider adding suffix to avoid default numeric fallback
98

109
error: default numeric fallback might occur
1110
--> $DIR/default_numeric_fallback.rs:11:18
1211
|
1312
LL | let x = [1, 2, 3];
14-
| ^
15-
|
16-
= help: consider adding suffix to avoid default numeric fallback
13+
| ^ help: consider adding suffix: `1_i32`
1714

1815
error: default numeric fallback might occur
1916
--> $DIR/default_numeric_fallback.rs:11:21
2017
|
2118
LL | let x = [1, 2, 3];
22-
| ^
23-
|
24-
= help: consider adding suffix to avoid default numeric fallback
19+
| ^ help: consider adding suffix: `2_i32`
2520

2621
error: default numeric fallback might occur
2722
--> $DIR/default_numeric_fallback.rs:11:24
2823
|
2924
LL | let x = [1, 2, 3];
30-
| ^
31-
|
32-
= help: consider adding suffix to avoid default numeric fallback
25+
| ^ help: consider adding suffix: `3_i32`
3326

3427
error: default numeric fallback might occur
3528
--> $DIR/default_numeric_fallback.rs:12:28
3629
|
3730
LL | let x = if true { (1, 2) } else { (3, 4) };
38-
| ^
39-
|
40-
= help: consider adding suffix to avoid default numeric fallback
31+
| ^ help: consider adding suffix: `1_i32`
4132

4233
error: default numeric fallback might occur
4334
--> $DIR/default_numeric_fallback.rs:12:31
4435
|
4536
LL | let x = if true { (1, 2) } else { (3, 4) };
46-
| ^
47-
|
48-
= help: consider adding suffix to avoid default numeric fallback
37+
| ^ help: consider adding suffix: `2_i32`
4938

5039
error: default numeric fallback might occur
5140
--> $DIR/default_numeric_fallback.rs:12:44
5241
|
5342
LL | let x = if true { (1, 2) } else { (3, 4) };
54-
| ^
55-
|
56-
= help: consider adding suffix to avoid default numeric fallback
43+
| ^ help: consider adding suffix: `3_i32`
5744

5845
error: default numeric fallback might occur
5946
--> $DIR/default_numeric_fallback.rs:12:47
6047
|
6148
LL | let x = if true { (1, 2) } else { (3, 4) };
62-
| ^
63-
|
64-
= help: consider adding suffix to avoid default numeric fallback
49+
| ^ help: consider adding suffix: `4_i32`
6550

6651
error: default numeric fallback might occur
6752
--> $DIR/default_numeric_fallback.rs:13:23
6853
|
6954
LL | let x = match 1 {
70-
| ^
71-
|
72-
= help: consider adding suffix to avoid default numeric fallback
55+
| ^ help: consider adding suffix: `1_i32`
7356

7457
error: default numeric fallback might occur
7558
--> $DIR/default_numeric_fallback.rs:14:13
7659
|
7760
LL | 1 => 1,
78-
| ^
79-
|
80-
= help: consider adding suffix to avoid default numeric fallback
61+
| ^ help: consider adding suffix: `1_i32`
8162

8263
error: default numeric fallback might occur
8364
--> $DIR/default_numeric_fallback.rs:14:18
8465
|
8566
LL | 1 => 1,
86-
| ^
87-
|
88-
= help: consider adding suffix to avoid default numeric fallback
67+
| ^ help: consider adding suffix: `1_i32`
8968

9069
error: default numeric fallback might occur
9170
--> $DIR/default_numeric_fallback.rs:15:18
9271
|
9372
LL | _ => 2,
94-
| ^
95-
|
96-
= help: consider adding suffix to avoid default numeric fallback
73+
| ^ help: consider adding suffix: `2_i32`
9774

9875
error: default numeric fallback might occur
9976
--> $DIR/default_numeric_fallback.rs:19:17
10077
|
10178
LL | let x = 0.12;
102-
| ^^^^
103-
|
104-
= help: consider adding suffix to avoid default numeric fallback
79+
| ^^^^ help: consider adding suffix: `0.12_f64`
10580

10681
error: default numeric fallback might occur
10782
--> $DIR/default_numeric_fallback.rs:37:21
10883
|
10984
LL | let y = 1;
110-
| ^
111-
|
112-
= help: consider adding suffix to avoid default numeric fallback
85+
| ^ help: consider adding suffix: `1_i32`
11386

11487
error: default numeric fallback might occur
11588
--> $DIR/default_numeric_fallback.rs:45:21
11689
|
11790
LL | let y = 1;
118-
| ^
119-
|
120-
= help: consider adding suffix to avoid default numeric fallback
91+
| ^ help: consider adding suffix: `1_i32`
12192

12293
error: default numeric fallback might occur
12394
--> $DIR/default_numeric_fallback.rs:51:21
12495
|
12596
LL | let y = 1;
126-
| ^
127-
|
128-
= help: consider adding suffix to avoid default numeric fallback
97+
| ^ help: consider adding suffix: `1_i32`
12998

13099
error: default numeric fallback might occur
131100
--> $DIR/default_numeric_fallback.rs:63:9
132101
|
133102
LL | 1
134-
| ^
135-
|
136-
= help: consider adding suffix to avoid default numeric fallback
103+
| ^ help: consider adding suffix: `1_i32`
137104

138105
error: default numeric fallback might occur
139106
--> $DIR/default_numeric_fallback.rs:69:27
140107
|
141108
LL | let f = || -> _ { 1 };
142-
| ^
143-
|
144-
= help: consider adding suffix to avoid default numeric fallback
109+
| ^ help: consider adding suffix: `1_i32`
145110

146111
error: default numeric fallback might occur
147112
--> $DIR/default_numeric_fallback.rs:73:29
148113
|
149114
LL | let f = || -> i32 { 1 };
150-
| ^
151-
|
152-
= help: consider adding suffix to avoid default numeric fallback
115+
| ^ help: consider adding suffix: `1_i32`
153116

154117
error: default numeric fallback might occur
155118
--> $DIR/default_numeric_fallback.rs:87:21
156119
|
157120
LL | generic_arg(1);
158-
| ^
159-
|
160-
= help: consider adding suffix to avoid default numeric fallback
121+
| ^ help: consider adding suffix: `1_i32`
161122

162123
error: default numeric fallback might occur
163124
--> $DIR/default_numeric_fallback.rs:90:32
164125
|
165126
LL | let x: _ = generic_arg(1);
166-
| ^
167-
|
168-
= help: consider adding suffix to avoid default numeric fallback
127+
| ^ help: consider adding suffix: `1_i32`
169128

170129
error: default numeric fallback might occur
171130
--> $DIR/default_numeric_fallback.rs:108:28
172131
|
173132
LL | GenericStruct { x: 1 };
174-
| ^
175-
|
176-
= help: consider adding suffix to avoid default numeric fallback
133+
| ^ help: consider adding suffix: `1_i32`
177134

178135
error: default numeric fallback might occur
179136
--> $DIR/default_numeric_fallback.rs:111:36
180137
|
181138
LL | let _ = GenericStruct { x: 1 };
182-
| ^
183-
|
184-
= help: consider adding suffix to avoid default numeric fallback
139+
| ^ help: consider adding suffix: `1_i32`
185140

186141
error: default numeric fallback might occur
187142
--> $DIR/default_numeric_fallback.rs:131:23
188143
|
189144
LL | s.generic_arg(1);
190-
| ^
191-
|
192-
= help: consider adding suffix to avoid default numeric fallback
145+
| ^ help: consider adding suffix: `1_i32`
193146

194147
error: aborting due to 24 previous errors
195148

0 commit comments

Comments
 (0)