Skip to content

Commit 25e4c7d

Browse files
committed
default_numeric_fallback: Add rustfix tests
1 parent 4e8cd4d commit 25e4c7d

7 files changed

+411
-53
lines changed

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::source::snippet;
2+
use clippy_utils::numeric_literal;
3+
use clippy_utils::source::snippet_opt;
34
use if_chain::if_chain;
45
use rustc_ast::ast::{LitFloatType, LitIntType, LitKind};
56
use rustc_errors::Applicability;
@@ -80,14 +81,23 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
8081
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed));
8182
if !ty_bound.is_numeric();
8283
then {
83-
let suffix = match lit_ty.kind() {
84-
ty::Int(IntTy::I32) => "i32",
85-
ty::Float(FloatTy::F64) => "f64",
84+
let (suffix, is_float) = match lit_ty.kind() {
85+
ty::Int(IntTy::I32) => ("i32", false),
86+
ty::Float(FloatTy::F64) => ("f64", true),
8687
// Default numeric fallback never results in other types.
8788
_ => return,
8889
};
8990

90-
let sugg = format!("{}_{}", snippet(self.cx, lit.span, ""), suffix);
91+
let src = if let Some(src) = snippet_opt(self.cx, lit.span) {
92+
src
93+
} else {
94+
match lit.node {
95+
LitKind::Int(src, _) => format!("{}", src),
96+
LitKind::Float(src, _) => format!("{}", src),
97+
_ => return,
98+
}
99+
};
100+
let sugg = numeric_literal::format(&src, Some(suffix), is_float);
91101
span_lint_and_sugg(
92102
self.cx,
93103
DEFAULT_NUMERIC_FALLBACK,
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
// run-rustfix
2+
// aux-build:macro_rules.rs
3+
4+
#![warn(clippy::default_numeric_fallback)]
5+
#![allow(unused)]
6+
#![allow(clippy::never_loop)]
7+
#![allow(clippy::no_effect)]
8+
#![allow(clippy::unnecessary_operation)]
9+
#![allow(clippy::branches_sharing_code)]
10+
#![allow(clippy::match_single_binding)]
11+
12+
#[macro_use]
13+
extern crate macro_rules;
14+
15+
mod basic_expr {
16+
fn test() {
17+
// Should lint unsuffixed literals typed `f64`.
18+
let x = 0.12_f64;
19+
let x = [1.0_f64, 2.0_f64, 3.0_f64];
20+
let x = if true { (1.0_f64, 2.0_f64) } else { (3.0_f64, 4.0_f64) };
21+
let x = match 1.0_f64 {
22+
_ => 1.0_f64,
23+
};
24+
25+
// Should NOT lint suffixed literals.
26+
let x = 0.12_f64;
27+
28+
// Should NOT lint literals in init expr if `Local` has a type annotation.
29+
let x: f64 = 0.1;
30+
let x: [f64; 3] = [1., 2., 3.];
31+
let x: (f64, f64) = if true { (1., 2.) } else { (3., 4.) };
32+
let x: _ = 1.;
33+
}
34+
}
35+
36+
mod nested_local {
37+
fn test() {
38+
let x: _ = {
39+
// Should lint this because this literal is not bound to any types.
40+
let y = 1.0_f64;
41+
42+
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
43+
1.
44+
};
45+
46+
let x: _ = if true {
47+
// Should lint this because this literal is not bound to any types.
48+
let y = 1.0_f64;
49+
50+
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
51+
1.
52+
} else {
53+
// Should lint this because this literal is not bound to any types.
54+
let y = 1.0_f64;
55+
56+
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
57+
2.
58+
};
59+
}
60+
}
61+
62+
mod function_def {
63+
fn ret_f64() -> f64 {
64+
// Even though the output type is specified,
65+
// this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
66+
1.0_f64
67+
}
68+
69+
fn test() {
70+
// Should lint this because return type is inferred to `f64` and NOT bound to a concrete
71+
// type.
72+
let f = || -> _ { 1.0_f64 };
73+
74+
// Even though the output type is specified,
75+
// this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
76+
let f = || -> f64 { 1.0_f64 };
77+
}
78+
}
79+
80+
mod function_calls {
81+
fn concrete_arg(f: f64) {}
82+
83+
fn generic_arg<T>(t: T) {}
84+
85+
fn test() {
86+
// Should NOT lint this because the argument type is bound to a concrete type.
87+
concrete_arg(1.);
88+
89+
// Should lint this because the argument type is inferred to `f64` and NOT bound to a concrete type.
90+
generic_arg(1.0_f64);
91+
92+
// Should lint this because the argument type is inferred to `f64` and NOT bound to a concrete type.
93+
let x: _ = generic_arg(1.0_f64);
94+
}
95+
}
96+
97+
mod struct_ctor {
98+
struct ConcreteStruct {
99+
x: f64,
100+
}
101+
102+
struct GenericStruct<T> {
103+
x: T,
104+
}
105+
106+
fn test() {
107+
// Should NOT lint this because the field type is bound to a concrete type.
108+
ConcreteStruct { x: 1. };
109+
110+
// Should lint this because the field type is inferred to `f64` and NOT bound to a concrete type.
111+
GenericStruct { x: 1.0_f64 };
112+
113+
// Should lint this because the field type is inferred to `f64` and NOT bound to a concrete type.
114+
let _ = GenericStruct { x: 1.0_f64 };
115+
}
116+
}
117+
118+
mod enum_ctor {
119+
enum ConcreteEnum {
120+
X(f64),
121+
}
122+
123+
enum GenericEnum<T> {
124+
X(T),
125+
}
126+
127+
fn test() {
128+
// Should NOT lint this because the field type is bound to a concrete type.
129+
ConcreteEnum::X(1.);
130+
131+
// Should lint this because the field type is inferred to `f64` and NOT bound to a concrete type.
132+
GenericEnum::X(1.0_f64);
133+
}
134+
}
135+
136+
mod method_calls {
137+
struct StructForMethodCallTest {}
138+
139+
impl StructForMethodCallTest {
140+
fn concrete_arg(&self, f: f64) {}
141+
142+
fn generic_arg<T>(&self, t: T) {}
143+
}
144+
145+
fn test() {
146+
let s = StructForMethodCallTest {};
147+
148+
// Should NOT lint this because the argument type is bound to a concrete type.
149+
s.concrete_arg(1.);
150+
151+
// Should lint this because the argument type is bound to a concrete type.
152+
s.generic_arg(1.0_f64);
153+
}
154+
}
155+
156+
mod in_macro {
157+
macro_rules! internal_macro {
158+
() => {
159+
let x = 22.0_f64;
160+
};
161+
}
162+
163+
// Should lint in internal macro.
164+
fn internal() {
165+
internal_macro!();
166+
}
167+
168+
// Should NOT lint in external macro.
169+
fn external() {
170+
default_numeric_fallback!();
171+
}
172+
}
173+
174+
fn main() {}

tests/ui/default_numeric_fallback_f64.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// run-rustfix
12
// aux-build:macro_rules.rs
23

34
#![warn(clippy::default_numeric_fallback)]
@@ -6,7 +7,6 @@
67
#![allow(clippy::no_effect)]
78
#![allow(clippy::unnecessary_operation)]
89
#![allow(clippy::branches_sharing_code)]
9-
#![allow(clippy::branches_sharing_code)]
1010
#![allow(clippy::match_single_binding)]
1111

1212
#[macro_use]

tests/ui/default_numeric_fallback_f64.stderr

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,133 +10,133 @@ error: default numeric fallback might occur
1010
--> $DIR/default_numeric_fallback_f64.rs:19:18
1111
|
1212
LL | let x = [1., 2., 3.];
13-
| ^^ help: consider adding suffix: `1._f64`
13+
| ^^ help: consider adding suffix: `1.0_f64`
1414

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

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

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

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

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

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

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

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

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

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

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

8181
error: default numeric fallback might occur
8282
--> $DIR/default_numeric_fallback_f64.rs:66:9
8383
|
8484
LL | 1.
85-
| ^^ help: consider adding suffix: `1._f64`
85+
| ^^ help: consider adding suffix: `1.0_f64`
8686

8787
error: default numeric fallback might occur
8888
--> $DIR/default_numeric_fallback_f64.rs:72:27
8989
|
9090
LL | let f = || -> _ { 1. };
91-
| ^^ help: consider adding suffix: `1._f64`
91+
| ^^ help: consider adding suffix: `1.0_f64`
9292

9393
error: default numeric fallback might occur
9494
--> $DIR/default_numeric_fallback_f64.rs:76:29
9595
|
9696
LL | let f = || -> f64 { 1. };
97-
| ^^ help: consider adding suffix: `1._f64`
97+
| ^^ help: consider adding suffix: `1.0_f64`
9898

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

105105
error: default numeric fallback might occur
106106
--> $DIR/default_numeric_fallback_f64.rs:93:32
107107
|
108108
LL | let x: _ = generic_arg(1.);
109-
| ^^ help: consider adding suffix: `1._f64`
109+
| ^^ help: consider adding suffix: `1.0_f64`
110110

111111
error: default numeric fallback might occur
112112
--> $DIR/default_numeric_fallback_f64.rs:111:28
113113
|
114114
LL | GenericStruct { x: 1. };
115-
| ^^ help: consider adding suffix: `1._f64`
115+
| ^^ help: consider adding suffix: `1.0_f64`
116116

117117
error: default numeric fallback might occur
118118
--> $DIR/default_numeric_fallback_f64.rs:114:36
119119
|
120120
LL | let _ = GenericStruct { x: 1. };
121-
| ^^ help: consider adding suffix: `1._f64`
121+
| ^^ help: consider adding suffix: `1.0_f64`
122122

123123
error: default numeric fallback might occur
124124
--> $DIR/default_numeric_fallback_f64.rs:132:24
125125
|
126126
LL | GenericEnum::X(1.);
127-
| ^^ help: consider adding suffix: `1._f64`
127+
| ^^ help: consider adding suffix: `1.0_f64`
128128

129129
error: default numeric fallback might occur
130130
--> $DIR/default_numeric_fallback_f64.rs:152:23
131131
|
132132
LL | s.generic_arg(1.);
133-
| ^^ help: consider adding suffix: `1._f64`
133+
| ^^ help: consider adding suffix: `1.0_f64`
134134

135135
error: default numeric fallback might occur
136136
--> $DIR/default_numeric_fallback_f64.rs:159:21
137137
|
138138
LL | let x = 22.;
139-
| ^^^ help: consider adding suffix: `22._f64`
139+
| ^^^ help: consider adding suffix: `22.0_f64`
140140
...
141141
LL | internal_macro!();
142142
| ------------------ in this macro invocation

0 commit comments

Comments
 (0)