Skip to content

Commit ec1a6cb

Browse files
committed
Auto merge of #3658 - detrumi:add-several-run-rustfix-annotations, r=phansch
Add several run rustfix annotations Adds `run-rustfix` to 18 of the tests from the tracking issue #3630. Each test has its own commit, to make reviewing easier (hopefully this is easier to review than 18 separate PRs). ## Changes - `cfg_attr_rustfmt`: Custom inner attributes are unstable. Let's disable the lint for inner attributes until [#54726](rust-lang/rust#54726) stabilizes - `collapsible_if`: unrelated cyclomatic_complexity warning that can be ignored - `duration_subsec`: Simply needed `#![allow(dead_code)]` - `excessive_precision`: Fixed by `#!allow(dead_code,unused_variables)` - `explicit_write`: Fixed by `#![allow(unused_imports)]` - `inconsistent_digit_grouping`: Avoid triggering `clippy::excessive_precision` lint - `infallible_destructuring_match`: Fixed by `#![allow(dead_code, unreachable_code, unused_variables)]` - `into_iter_on_ref`: Triggered unrelated `clippy::useless_vec` lint - `large_digit_groups`: Avoid triggering `clippy::excessive_precision` lint - `map_clone`: Fixed by `#![allow(clippy::iter_cloned_collect)]` - `mem_replace`: Suggestion causes import to be unused, fixed by `#![allow(unused_imports)]` - `precedence`: Allow some unrelated lints, and change out-of-range `0b1111_1111i8` literal - `redundant_field_names`: Allow dead code, and remove stabilized feature toggles - `replace_consts`: Fixed by `#![allow(unused_variables)]` - `starts_ends_with`: Fixed by `#![allow(unused_must_use)]` - `types`: Fixed by `#![allow(dead_code, unused_variables)]` - `unit_arg`: Fixed by `#[allow(unused_must_use)]` - `unnecessary_fold`: Fixed by adding type annotations and adding `#![allow(dead_code)]`
2 parents ac10c56 + 51c0dd4 commit ec1a6cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1137
-202
lines changed

clippy_lints/src/attrs.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -511,18 +511,17 @@ impl EarlyLintPass for CfgAttrPass {
511511
// check for `rustfmt_skip` and `rustfmt::skip`
512512
if let Some(skip_item) = &items[1].meta_item();
513513
if skip_item.name() == "rustfmt_skip" || skip_item.name() == "skip";
514+
// Only lint outer attributes, because custom inner attributes are unstable
515+
// Tracking issue: https://github.com/rust-lang/rust/issues/54726
516+
if let AttrStyle::Outer = attr.style;
514517
then {
515-
let attr_style = match attr.style {
516-
AttrStyle::Outer => "#[",
517-
AttrStyle::Inner => "#![",
518-
};
519518
span_lint_and_sugg(
520519
cx,
521520
DEPRECATED_CFG_ATTR,
522521
attr.span,
523522
"`cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes",
524523
"use",
525-
format!("{}rustfmt::skip]", attr_style),
524+
"#[rustfmt::skip]".to_string(),
526525
Applicability::MachineApplicable,
527526
);
528527
}

tests/ui/cfg_attr_rustfmt.fixed

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// run-rustfix
2+
#![feature(stmt_expr_attributes)]
3+
4+
#![allow(unused, clippy::no_effect)]
5+
#![warn(clippy::deprecated_cfg_attr)]
6+
7+
// This doesn't get linted, see known problems
8+
#![cfg_attr(rustfmt, rustfmt_skip)]
9+
10+
#[rustfmt::skip]
11+
trait Foo
12+
{
13+
fn foo(
14+
);
15+
}
16+
17+
fn skip_on_statements() {
18+
#[rustfmt::skip]
19+
5+3;
20+
}
21+
22+
#[rustfmt::skip]
23+
fn main() {
24+
foo::f();
25+
}
26+
27+
mod foo {
28+
#![cfg_attr(rustfmt, rustfmt_skip)]
29+
30+
pub fn f() {}
31+
}

tests/ui/cfg_attr_rustfmt.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
// run-rustfix
12
#![feature(stmt_expr_attributes)]
23

4+
#![allow(unused, clippy::no_effect)]
35
#![warn(clippy::deprecated_cfg_attr)]
46

57
// This doesn't get linted, see known problems

tests/ui/cfg_attr_rustfmt.stderr

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
error: `cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes
2-
--> $DIR/cfg_attr_rustfmt.rs:16:5
2+
--> $DIR/cfg_attr_rustfmt.rs:18:5
33
|
44
LL | #[cfg_attr(rustfmt, rustfmt::skip)]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `#[rustfmt::skip]`
66
|
77
= note: `-D clippy::deprecated-cfg-attr` implied by `-D warnings`
88

99
error: `cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes
10-
--> $DIR/cfg_attr_rustfmt.rs:20:1
10+
--> $DIR/cfg_attr_rustfmt.rs:22:1
1111
|
1212
LL | #[cfg_attr(rustfmt, rustfmt_skip)]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `#[rustfmt::skip]`
1414

15-
error: `cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes
16-
--> $DIR/cfg_attr_rustfmt.rs:26:5
17-
|
18-
LL | #![cfg_attr(rustfmt, rustfmt_skip)]
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `#![rustfmt::skip]`
20-
21-
error: aborting due to 3 previous errors
15+
error: aborting due to 2 previous errors
2216

tests/ui/collapsible_if.fixed

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
// run-rustfix
2+
#![allow(clippy::cyclomatic_complexity)]
3+
4+
#[rustfmt::skip]
5+
#[warn(clippy::collapsible_if)]
6+
fn main() {
7+
let x = "hello";
8+
let y = "world";
9+
if x == "hello" && y == "world" {
10+
println!("Hello world!");
11+
}
12+
13+
if (x == "hello" || x == "world") && (y == "world" || y == "hello") {
14+
println!("Hello world!");
15+
}
16+
17+
if x == "hello" && x == "world" && (y == "world" || y == "hello") {
18+
println!("Hello world!");
19+
}
20+
21+
if (x == "hello" || x == "world") && y == "world" && y == "hello" {
22+
println!("Hello world!");
23+
}
24+
25+
if x == "hello" && x == "world" && y == "world" && y == "hello" {
26+
println!("Hello world!");
27+
}
28+
29+
if 42 == 1337 && 'a' != 'A' {
30+
println!("world!")
31+
}
32+
33+
// Collapse `else { if .. }` to `else if ..`
34+
if x == "hello" {
35+
print!("Hello ");
36+
} else if y == "world" {
37+
println!("world!")
38+
}
39+
40+
if x == "hello" {
41+
print!("Hello ");
42+
} else if let Some(42) = Some(42) {
43+
println!("world!")
44+
}
45+
46+
if x == "hello" {
47+
print!("Hello ");
48+
} else if y == "world" {
49+
println!("world")
50+
}
51+
else {
52+
println!("!")
53+
}
54+
55+
if x == "hello" {
56+
print!("Hello ");
57+
} else if let Some(42) = Some(42) {
58+
println!("world")
59+
}
60+
else {
61+
println!("!")
62+
}
63+
64+
if let Some(42) = Some(42) {
65+
print!("Hello ");
66+
} else if let Some(42) = Some(42) {
67+
println!("world")
68+
}
69+
else {
70+
println!("!")
71+
}
72+
73+
if let Some(42) = Some(42) {
74+
print!("Hello ");
75+
} else if x == "hello" {
76+
println!("world")
77+
}
78+
else {
79+
println!("!")
80+
}
81+
82+
if let Some(42) = Some(42) {
83+
print!("Hello ");
84+
} else if let Some(42) = Some(42) {
85+
println!("world")
86+
}
87+
else {
88+
println!("!")
89+
}
90+
91+
// Works because any if with an else statement cannot be collapsed.
92+
if x == "hello" {
93+
if y == "world" {
94+
println!("Hello world!");
95+
}
96+
} else {
97+
println!("Not Hello world");
98+
}
99+
100+
if x == "hello" {
101+
if y == "world" {
102+
println!("Hello world!");
103+
} else {
104+
println!("Hello something else");
105+
}
106+
}
107+
108+
if x == "hello" {
109+
print!("Hello ");
110+
if y == "world" {
111+
println!("world!")
112+
}
113+
}
114+
115+
if true {
116+
} else {
117+
assert!(true); // assert! is just an `if`
118+
}
119+
120+
121+
// The following tests check for the fix of https://github.com/rust-lang/rust-clippy/issues/798
122+
if x == "hello" {// Not collapsible
123+
if y == "world" {
124+
println!("Hello world!");
125+
}
126+
}
127+
128+
if x == "hello" { // Not collapsible
129+
if y == "world" {
130+
println!("Hello world!");
131+
}
132+
}
133+
134+
if x == "hello" {
135+
// Not collapsible
136+
if y == "world" {
137+
println!("Hello world!");
138+
}
139+
}
140+
141+
if x == "hello" && y == "world" { // Collapsible
142+
println!("Hello world!");
143+
}
144+
145+
if x == "hello" {
146+
print!("Hello ");
147+
} else {
148+
// Not collapsible
149+
if y == "world" {
150+
println!("world!")
151+
}
152+
}
153+
154+
if x == "hello" {
155+
print!("Hello ");
156+
} else {
157+
// Not collapsible
158+
if let Some(42) = Some(42) {
159+
println!("world!")
160+
}
161+
}
162+
163+
if x == "hello" {
164+
/* Not collapsible */
165+
if y == "world" {
166+
println!("Hello world!");
167+
}
168+
}
169+
170+
if x == "hello" { /* Not collapsible */
171+
if y == "world" {
172+
println!("Hello world!");
173+
}
174+
}
175+
}

tests/ui/collapsible_if.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// run-rustfix
2+
#![allow(clippy::cyclomatic_complexity)]
3+
14
#[rustfmt::skip]
25
#[warn(clippy::collapsible_if)]
36
fn main() {

tests/ui/collapsible_if.stderr

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this if statement can be collapsed
2-
--> $DIR/collapsible_if.rs:6:5
2+
--> $DIR/collapsible_if.rs:9:5
33
|
44
LL | / if x == "hello" {
55
LL | | if y == "world" {
@@ -17,7 +17,7 @@ LL | }
1717
|
1818

1919
error: this if statement can be collapsed
20-
--> $DIR/collapsible_if.rs:12:5
20+
--> $DIR/collapsible_if.rs:15:5
2121
|
2222
LL | / if x == "hello" || x == "world" {
2323
LL | | if y == "world" || y == "hello" {
@@ -33,7 +33,7 @@ LL | }
3333
|
3434

3535
error: this if statement can be collapsed
36-
--> $DIR/collapsible_if.rs:18:5
36+
--> $DIR/collapsible_if.rs:21:5
3737
|
3838
LL | / if x == "hello" && x == "world" {
3939
LL | | if y == "world" || y == "hello" {
@@ -49,7 +49,7 @@ LL | }
4949
|
5050

5151
error: this if statement can be collapsed
52-
--> $DIR/collapsible_if.rs:24:5
52+
--> $DIR/collapsible_if.rs:27:5
5353
|
5454
LL | / if x == "hello" || x == "world" {
5555
LL | | if y == "world" && y == "hello" {
@@ -65,7 +65,7 @@ LL | }
6565
|
6666

6767
error: this if statement can be collapsed
68-
--> $DIR/collapsible_if.rs:30:5
68+
--> $DIR/collapsible_if.rs:33:5
6969
|
7070
LL | / if x == "hello" && x == "world" {
7171
LL | | if y == "world" && y == "hello" {
@@ -81,7 +81,7 @@ LL | }
8181
|
8282

8383
error: this if statement can be collapsed
84-
--> $DIR/collapsible_if.rs:36:5
84+
--> $DIR/collapsible_if.rs:39:5
8585
|
8686
LL | / if 42 == 1337 {
8787
LL | | if 'a' != 'A' {
@@ -97,7 +97,7 @@ LL | }
9797
|
9898

9999
error: this `else { if .. }` block can be collapsed
100-
--> $DIR/collapsible_if.rs:45:12
100+
--> $DIR/collapsible_if.rs:48:12
101101
|
102102
LL | } else {
103103
| ____________^
@@ -114,7 +114,7 @@ LL | }
114114
|
115115

116116
error: this `else { if .. }` block can be collapsed
117-
--> $DIR/collapsible_if.rs:53:12
117+
--> $DIR/collapsible_if.rs:56:12
118118
|
119119
LL | } else {
120120
| ____________^
@@ -131,7 +131,7 @@ LL | }
131131
|
132132

133133
error: this `else { if .. }` block can be collapsed
134-
--> $DIR/collapsible_if.rs:61:12
134+
--> $DIR/collapsible_if.rs:64:12
135135
|
136136
LL | } else {
137137
| ____________^
@@ -153,7 +153,7 @@ LL | }
153153
|
154154

155155
error: this `else { if .. }` block can be collapsed
156-
--> $DIR/collapsible_if.rs:72:12
156+
--> $DIR/collapsible_if.rs:75:12
157157
|
158158
LL | } else {
159159
| ____________^
@@ -175,7 +175,7 @@ LL | }
175175
|
176176

177177
error: this `else { if .. }` block can be collapsed
178-
--> $DIR/collapsible_if.rs:83:12
178+
--> $DIR/collapsible_if.rs:86:12
179179
|
180180
LL | } else {
181181
| ____________^
@@ -197,7 +197,7 @@ LL | }
197197
|
198198

199199
error: this `else { if .. }` block can be collapsed
200-
--> $DIR/collapsible_if.rs:94:12
200+
--> $DIR/collapsible_if.rs:97:12
201201
|
202202
LL | } else {
203203
| ____________^
@@ -219,7 +219,7 @@ LL | }
219219
|
220220

221221
error: this `else { if .. }` block can be collapsed
222-
--> $DIR/collapsible_if.rs:105:12
222+
--> $DIR/collapsible_if.rs:108:12
223223
|
224224
LL | } else {
225225
| ____________^
@@ -241,7 +241,7 @@ LL | }
241241
|
242242

243243
error: this if statement can be collapsed
244-
--> $DIR/collapsible_if.rs:164:5
244+
--> $DIR/collapsible_if.rs:167:5
245245
|
246246
LL | / if x == "hello" {
247247
LL | | if y == "world" { // Collapsible

0 commit comments

Comments
 (0)