Skip to content

Commit

Permalink
Add check for empty cfg all condition
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed May 19, 2023
1 parent 5328852 commit 94db4b0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
8 changes: 8 additions & 0 deletions clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,14 @@ fn check_nested_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
}
},
);
} else if list.is_empty() && meta.has_name(sym::all) {
span_lint_and_then(
cx,
NON_MINIMAL_CFG,
meta.span,
"unneeded sub `cfg` when there is no condition",
|_| {},
);
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/non_minimal_cfg.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ fn wasi() {}
#[cfg(all(unix, not(windows)))]
fn the_end() {}

#[cfg(all())]
fn all() {}

#[cfg(any())]
fn any() {}

fn main() {}
8 changes: 6 additions & 2 deletions tests/ui/non_minimal_cfg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//@run-rustfix

#![allow(unused)]

#[cfg(all(windows))]
Expand All @@ -11,4 +9,10 @@ fn wasi() {}
#[cfg(all(any(unix), all(not(windows))))]
fn the_end() {}

#[cfg(all())]
fn all() {}

#[cfg(any())]
fn any() {}

fn main() {}
16 changes: 11 additions & 5 deletions tests/ui/non_minimal_cfg.stderr
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
error: unneeded sub `cfg` when there is only one condition
--> $DIR/non_minimal_cfg.rs:5:7
--> $DIR/non_minimal_cfg.rs:3:7
|
LL | #[cfg(all(windows))]
| ^^^^^^^^^^^^ help: try: `windows`
|
= note: `-D clippy::non-minimal-cfg` implied by `-D warnings`

error: unneeded sub `cfg` when there is only one condition
--> $DIR/non_minimal_cfg.rs:8:7
--> $DIR/non_minimal_cfg.rs:6:7
|
LL | #[cfg(any(windows))]
| ^^^^^^^^^^^^ help: try: `windows`

error: unneeded sub `cfg` when there is only one condition
--> $DIR/non_minimal_cfg.rs:11:11
--> $DIR/non_minimal_cfg.rs:9:11
|
LL | #[cfg(all(any(unix), all(not(windows))))]
| ^^^^^^^^^ help: try: `unix`

error: unneeded sub `cfg` when there is only one condition
--> $DIR/non_minimal_cfg.rs:11:22
--> $DIR/non_minimal_cfg.rs:9:22
|
LL | #[cfg(all(any(unix), all(not(windows))))]
| ^^^^^^^^^^^^^^^^^ help: try: `not(windows)`

error: aborting due to 4 previous errors
error: unneeded sub `cfg` when there is no condition
--> $DIR/non_minimal_cfg.rs:12:7
|
LL | #[cfg(all())]
| ^^^^^

error: aborting due to 5 previous errors

0 comments on commit 94db4b0

Please sign in to comment.