Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add several run rustfix annotations #3658

Merged
merged 18 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
phansch marked this conversation as resolved.
Show resolved Hide resolved
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