Skip to content

Commit

Permalink
Auto merge of #3658 - detrumi:add-several-run-rustfix-annotations, r=…
Browse files Browse the repository at this point in the history
…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)]`
  • Loading branch information
bors committed Jan 14, 2019
2 parents ac10c56 + 51c0dd4 commit ec1a6cb
Show file tree
Hide file tree
Showing 54 changed files with 1,137 additions and 202 deletions.
9 changes: 4 additions & 5 deletions clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,18 +511,17 @@ impl EarlyLintPass for CfgAttrPass {
// check for `rustfmt_skip` and `rustfmt::skip`
if let Some(skip_item) = &items[1].meta_item();
if skip_item.name() == "rustfmt_skip" || skip_item.name() == "skip";
// Only lint outer attributes, because custom inner attributes are unstable
// Tracking issue: https://github.com/rust-lang/rust/issues/54726
if let AttrStyle::Outer = attr.style;
then {
let attr_style = match attr.style {
AttrStyle::Outer => "#[",
AttrStyle::Inner => "#![",
};
span_lint_and_sugg(
cx,
DEPRECATED_CFG_ATTR,
attr.span,
"`cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes",
"use",
format!("{}rustfmt::skip]", attr_style),
"#[rustfmt::skip]".to_string(),
Applicability::MachineApplicable,
);
}
Expand Down
31 changes: 31 additions & 0 deletions tests/ui/cfg_attr_rustfmt.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// run-rustfix
#![feature(stmt_expr_attributes)]

#![allow(unused, clippy::no_effect)]
#![warn(clippy::deprecated_cfg_attr)]

// This doesn't get linted, see known problems
#![cfg_attr(rustfmt, rustfmt_skip)]

#[rustfmt::skip]
trait Foo
{
fn foo(
);
}

fn skip_on_statements() {
#[rustfmt::skip]
5+3;
}

#[rustfmt::skip]
fn main() {
foo::f();
}

mod foo {
#![cfg_attr(rustfmt, rustfmt_skip)]

pub fn f() {}
}
2 changes: 2 additions & 0 deletions tests/ui/cfg_attr_rustfmt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// run-rustfix
#![feature(stmt_expr_attributes)]

#![allow(unused, clippy::no_effect)]
#![warn(clippy::deprecated_cfg_attr)]

// This doesn't get linted, see known problems
Expand Down
12 changes: 3 additions & 9 deletions tests/ui/cfg_attr_rustfmt.stderr
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
error: `cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes
--> $DIR/cfg_attr_rustfmt.rs:16:5
--> $DIR/cfg_attr_rustfmt.rs:18:5
|
LL | #[cfg_attr(rustfmt, rustfmt::skip)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `#[rustfmt::skip]`
|
= note: `-D clippy::deprecated-cfg-attr` implied by `-D warnings`

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

error: `cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes
--> $DIR/cfg_attr_rustfmt.rs:26:5
|
LL | #![cfg_attr(rustfmt, rustfmt_skip)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `#![rustfmt::skip]`

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

175 changes: 175 additions & 0 deletions tests/ui/collapsible_if.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// run-rustfix
#![allow(clippy::cyclomatic_complexity)]

#[rustfmt::skip]
#[warn(clippy::collapsible_if)]
fn main() {
let x = "hello";
let y = "world";
if x == "hello" && y == "world" {
println!("Hello world!");
}

if (x == "hello" || x == "world") && (y == "world" || y == "hello") {
println!("Hello world!");
}

if x == "hello" && x == "world" && (y == "world" || y == "hello") {
println!("Hello world!");
}

if (x == "hello" || x == "world") && y == "world" && y == "hello" {
println!("Hello world!");
}

if x == "hello" && x == "world" && y == "world" && y == "hello" {
println!("Hello world!");
}

if 42 == 1337 && 'a' != 'A' {
println!("world!")
}

// Collapse `else { if .. }` to `else if ..`
if x == "hello" {
print!("Hello ");
} else if y == "world" {
println!("world!")
}

if x == "hello" {
print!("Hello ");
} else if let Some(42) = Some(42) {
println!("world!")
}

if x == "hello" {
print!("Hello ");
} else if y == "world" {
println!("world")
}
else {
println!("!")
}

if x == "hello" {
print!("Hello ");
} else if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}

if let Some(42) = Some(42) {
print!("Hello ");
} else if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}

if let Some(42) = Some(42) {
print!("Hello ");
} else if x == "hello" {
println!("world")
}
else {
println!("!")
}

if let Some(42) = Some(42) {
print!("Hello ");
} else if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}

// Works because any if with an else statement cannot be collapsed.
if x == "hello" {
if y == "world" {
println!("Hello world!");
}
} else {
println!("Not Hello world");
}

if x == "hello" {
if y == "world" {
println!("Hello world!");
} else {
println!("Hello something else");
}
}

if x == "hello" {
print!("Hello ");
if y == "world" {
println!("world!")
}
}

if true {
} else {
assert!(true); // assert! is just an `if`
}


// The following tests check for the fix of https://github.com/rust-lang/rust-clippy/issues/798
if x == "hello" {// Not collapsible
if y == "world" {
println!("Hello world!");
}
}

if x == "hello" { // Not collapsible
if y == "world" {
println!("Hello world!");
}
}

if x == "hello" {
// Not collapsible
if y == "world" {
println!("Hello world!");
}
}

if x == "hello" && y == "world" { // Collapsible
println!("Hello world!");
}

if x == "hello" {
print!("Hello ");
} else {
// Not collapsible
if y == "world" {
println!("world!")
}
}

if x == "hello" {
print!("Hello ");
} else {
// Not collapsible
if let Some(42) = Some(42) {
println!("world!")
}
}

if x == "hello" {
/* Not collapsible */
if y == "world" {
println!("Hello world!");
}
}

if x == "hello" { /* Not collapsible */
if y == "world" {
println!("Hello world!");
}
}
}
3 changes: 3 additions & 0 deletions tests/ui/collapsible_if.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// run-rustfix
#![allow(clippy::cyclomatic_complexity)]

#[rustfmt::skip]
#[warn(clippy::collapsible_if)]
fn main() {
Expand Down
28 changes: 14 additions & 14 deletions tests/ui/collapsible_if.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this if statement can be collapsed
--> $DIR/collapsible_if.rs:6:5
--> $DIR/collapsible_if.rs:9:5
|
LL | / if x == "hello" {
LL | | if y == "world" {
Expand All @@ -17,7 +17,7 @@ LL | }
|

error: this if statement can be collapsed
--> $DIR/collapsible_if.rs:12:5
--> $DIR/collapsible_if.rs:15:5
|
LL | / if x == "hello" || x == "world" {
LL | | if y == "world" || y == "hello" {
Expand All @@ -33,7 +33,7 @@ LL | }
|

error: this if statement can be collapsed
--> $DIR/collapsible_if.rs:18:5
--> $DIR/collapsible_if.rs:21:5
|
LL | / if x == "hello" && x == "world" {
LL | | if y == "world" || y == "hello" {
Expand All @@ -49,7 +49,7 @@ LL | }
|

error: this if statement can be collapsed
--> $DIR/collapsible_if.rs:24:5
--> $DIR/collapsible_if.rs:27:5
|
LL | / if x == "hello" || x == "world" {
LL | | if y == "world" && y == "hello" {
Expand All @@ -65,7 +65,7 @@ LL | }
|

error: this if statement can be collapsed
--> $DIR/collapsible_if.rs:30:5
--> $DIR/collapsible_if.rs:33:5
|
LL | / if x == "hello" && x == "world" {
LL | | if y == "world" && y == "hello" {
Expand All @@ -81,7 +81,7 @@ LL | }
|

error: this if statement can be collapsed
--> $DIR/collapsible_if.rs:36:5
--> $DIR/collapsible_if.rs:39:5
|
LL | / if 42 == 1337 {
LL | | if 'a' != 'A' {
Expand All @@ -97,7 +97,7 @@ LL | }
|

error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_if.rs:45:12
--> $DIR/collapsible_if.rs:48:12
|
LL | } else {
| ____________^
Expand All @@ -114,7 +114,7 @@ LL | }
|

error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_if.rs:53:12
--> $DIR/collapsible_if.rs:56:12
|
LL | } else {
| ____________^
Expand All @@ -131,7 +131,7 @@ LL | }
|

error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_if.rs:61:12
--> $DIR/collapsible_if.rs:64:12
|
LL | } else {
| ____________^
Expand All @@ -153,7 +153,7 @@ LL | }
|

error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_if.rs:72:12
--> $DIR/collapsible_if.rs:75:12
|
LL | } else {
| ____________^
Expand All @@ -175,7 +175,7 @@ LL | }
|

error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_if.rs:83:12
--> $DIR/collapsible_if.rs:86:12
|
LL | } else {
| ____________^
Expand All @@ -197,7 +197,7 @@ LL | }
|

error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_if.rs:94:12
--> $DIR/collapsible_if.rs:97:12
|
LL | } else {
| ____________^
Expand All @@ -219,7 +219,7 @@ LL | }
|

error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_if.rs:105:12
--> $DIR/collapsible_if.rs:108:12
|
LL | } else {
| ____________^
Expand All @@ -241,7 +241,7 @@ LL | }
|

error: this if statement can be collapsed
--> $DIR/collapsible_if.rs:164:5
--> $DIR/collapsible_if.rs:167:5
|
LL | / if x == "hello" {
LL | | if y == "world" { // Collapsible
Expand Down
Loading

0 comments on commit ec1a6cb

Please sign in to comment.