Skip to content

Commit ea1c733

Browse files
Add ui test for new misleading_cfg_in_build_script lint
1 parent 6583dcd commit ea1c733

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// This test ensures that the `misleading_cfg_in_build_script` is not emitted if not
2+
// in a cargo `build.rs` script.
3+
//
4+
//@ no-auto-check-cfg
5+
//@ check-pass
6+
7+
#![deny(misleading_cfg_in_build_script)]
8+
#![allow(dead_code)]
9+
10+
#[cfg(windows)]
11+
fn unused_windows_fn() {}
12+
#[cfg(not(windows))]
13+
fn unused_not_windows_fn() {}
14+
#[cfg(any(windows, feature = "yellow", unix))]
15+
fn pink() {}
16+
17+
#[cfg(feature = "green")]
18+
fn pink() {}
19+
#[cfg(bob)]
20+
fn bob() {}
21+
22+
fn main() {
23+
if cfg!(windows) {}
24+
if cfg!(not(windows)) {}
25+
if cfg!(target_os = "freebsd") {}
26+
if cfg!(any(target_os = "freebsd", windows)) {}
27+
if cfg!(not(any(target_os = "freebsd", windows))) {}
28+
if cfg!(all(target_os = "freebsd", windows)) {}
29+
if cfg!(not(all(target_os = "freebsd", windows))) {}
30+
if cfg!(all(target_os = "freebsd", any(windows, not(feature = "red")))) {}
31+
32+
if cfg!(any()) {}
33+
if cfg!(all()) {}
34+
if cfg!(feature = "blue") {}
35+
if cfg!(bob) {}
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// This test checks the `cfg()` attributes/macros in cargo build scripts.
2+
//
3+
//@ no-auto-check-cfg
4+
//@ rustc-env:CARGO_CRATE_NAME=build_script_build
5+
//@ compile-flags:--crate-name=build_script_build
6+
7+
#![deny(misleading_cfg_in_build_script)]
8+
#![allow(dead_code)]
9+
10+
#[cfg(windows)]
11+
//~^ ERROR: misleading_cfg_in_build_script
12+
fn unused_windows_fn() {}
13+
#[cfg(not(windows))]
14+
//~^ ERROR: misleading_cfg_in_build_script
15+
fn unused_not_windows_fn() {}
16+
#[cfg(any(windows, feature = "yellow", unix))]
17+
//~^ ERROR: misleading_cfg_in_build_script
18+
fn pink() {}
19+
20+
// Should not lint.
21+
#[cfg(feature = "green")]
22+
fn pink() {}
23+
#[cfg(bob)]
24+
fn bob() {}
25+
26+
fn main() {
27+
if cfg!(windows) {}
28+
//~^ ERROR: misleading_cfg_in_build_script
29+
if cfg!(not(windows)) {}
30+
//~^ ERROR: misleading_cfg_in_build_script
31+
if cfg!(target_os = "freebsd") {}
32+
//~^ ERROR: misleading_cfg_in_build_script
33+
if cfg!(any(target_os = "freebsd", windows)) {}
34+
//~^ ERROR: misleading_cfg_in_build_script
35+
if cfg!(not(any(target_os = "freebsd", windows))) {}
36+
//~^ ERROR: misleading_cfg_in_build_script
37+
if cfg!(all(target_os = "freebsd", windows)) {}
38+
//~^ ERROR: misleading_cfg_in_build_script
39+
if cfg!(not(all(target_os = "freebsd", windows))) {}
40+
//~^ ERROR: misleading_cfg_in_build_script
41+
if cfg!(all(target_os = "freebsd", any(windows, not(feature = "red")))) {}
42+
//~^ ERROR: misleading_cfg_in_build_script
43+
44+
// Should not warn.
45+
if cfg!(any()) {}
46+
if cfg!(all()) {}
47+
if cfg!(feature = "blue") {}
48+
if cfg!(bob) {}
49+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
error: target-based cfg should be avoided in build scripts
2+
--> $DIR/misleading_cfg_in_build_script.rs:10:1
3+
|
4+
LL | #[cfg(windows)]
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/misleading_cfg_in_build_script.rs:7:9
9+
|
10+
LL | #![deny(misleading_cfg_in_build_script)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: target-based cfg should be avoided in build scripts
14+
--> $DIR/misleading_cfg_in_build_script.rs:13:1
15+
|
16+
LL | #[cfg(not(windows))]
17+
| ^^^^^^^^^^^^^^^^^^^^
18+
19+
error: target-based cfg should be avoided in build scripts
20+
--> $DIR/misleading_cfg_in_build_script.rs:16:11
21+
|
22+
LL | #[cfg(any(windows, feature = "yellow", unix))]
23+
| ^^^^^^^ ^^^^
24+
25+
error: target-based cfg should be avoided in build scripts
26+
--> $DIR/misleading_cfg_in_build_script.rs:27:8
27+
|
28+
LL | if cfg!(windows) {}
29+
| ^^^^^^^^^^^^^ help: use cargo environment variables if possible: `std::env::var("CARGO_CFG_WINDOWS").is_ok()`
30+
31+
error: target-based cfg should be avoided in build scripts
32+
--> $DIR/misleading_cfg_in_build_script.rs:29:8
33+
|
34+
LL | if cfg!(not(windows)) {}
35+
| ^^^^^^^^^^^^^^^^^^ help: use cargo environment variables if possible: `std::env::var("CARGO_CFG_WINDOWS").is_err()`
36+
37+
error: target-based cfg should be avoided in build scripts
38+
--> $DIR/misleading_cfg_in_build_script.rs:31:8
39+
|
40+
LL | if cfg!(target_os = "freebsd") {}
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use cargo environment variables if possible: `std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default() == "freebsd"`
42+
43+
error: target-based cfg should be avoided in build scripts
44+
--> $DIR/misleading_cfg_in_build_script.rs:33:8
45+
|
46+
LL | if cfg!(any(target_os = "freebsd", windows)) {}
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use cargo environment variables if possible: `std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default() == "freebsd" || std::env::var("CARGO_CFG_WINDOWS").is_ok()`
48+
49+
error: target-based cfg should be avoided in build scripts
50+
--> $DIR/misleading_cfg_in_build_script.rs:35:8
51+
|
52+
LL | if cfg!(not(any(target_os = "freebsd", windows))) {}
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use cargo environment variables if possible: `!(std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default() == "freebsd" || std::env::var("CARGO_CFG_WINDOWS").is_ok())`
54+
55+
error: target-based cfg should be avoided in build scripts
56+
--> $DIR/misleading_cfg_in_build_script.rs:37:8
57+
|
58+
LL | if cfg!(all(target_os = "freebsd", windows)) {}
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use cargo environment variables if possible: `std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default() == "freebsd" && std::env::var("CARGO_CFG_WINDOWS").is_ok()`
60+
61+
error: target-based cfg should be avoided in build scripts
62+
--> $DIR/misleading_cfg_in_build_script.rs:39:8
63+
|
64+
LL | if cfg!(not(all(target_os = "freebsd", windows))) {}
65+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use cargo environment variables if possible: `!(std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default() == "freebsd" && std::env::var("CARGO_CFG_WINDOWS").is_ok())`
66+
67+
error: target-based cfg should be avoided in build scripts
68+
--> $DIR/misleading_cfg_in_build_script.rs:41:8
69+
|
70+
LL | if cfg!(all(target_os = "freebsd", any(windows, not(feature = "red")))) {}
71+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use cargo environment variables if possible: `std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default() == "freebsd" && (std::env::var("CARGO_CFG_WINDOWS").is_ok() || cfg!(not(feature = red)))`
72+
73+
error: aborting due to 11 previous errors
74+

0 commit comments

Comments
 (0)