|
| 1 | +//! Tests for overriding warning behavior using `build.warnings` config option. |
| 2 | +
|
| 3 | +use cargo_test_support::{cargo_test, project, str, tools, Project}; |
| 4 | + |
| 5 | +fn make_project_with_rustc_warning() -> Project { |
| 6 | + project() |
| 7 | + .file( |
| 8 | + "Cargo.toml", |
| 9 | + &format!( |
| 10 | + r#" |
| 11 | + [package] |
| 12 | + name = "foo" |
| 13 | + version = "0.0.1" |
| 14 | + edition = "2021" |
| 15 | + "# |
| 16 | + ), |
| 17 | + ) |
| 18 | + .file("src/main.rs", "fn main() { let x = 3; }") |
| 19 | + .build() |
| 20 | +} |
| 21 | + |
| 22 | +#[cargo_test] |
| 23 | +fn rustc_caching_allow_first() { |
| 24 | + let p = make_project_with_rustc_warning(); |
| 25 | + p.cargo("check") |
| 26 | + .with_stderr_data(str![[r#" |
| 27 | +[CHECKING] foo v0.0.1 ([ROOT]/foo) |
| 28 | +[WARNING] unused variable: `x` |
| 29 | + --> src/main.rs:1:17 |
| 30 | + | |
| 31 | +1 | fn main() { let x = 3; } |
| 32 | + | ^ [HELP] if this is intentional, prefix it with an underscore: `_x` |
| 33 | + | |
| 34 | + = [NOTE] `#[warn(unused_variables)]` on by default |
| 35 | +
|
| 36 | +[WARNING] `foo` (bin "foo") generated 1 warning |
| 37 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 38 | +
|
| 39 | +"#]]) |
| 40 | + .run(); |
| 41 | + |
| 42 | + p.cargo("check") |
| 43 | + .with_stderr_data(str![[r#" |
| 44 | +[WARNING] unused variable: `x` |
| 45 | + --> src/main.rs:1:17 |
| 46 | + | |
| 47 | +1 | fn main() { let x = 3; } |
| 48 | + | ^ [HELP] if this is intentional, prefix it with an underscore: `_x` |
| 49 | + | |
| 50 | + = [NOTE] `#[warn(unused_variables)]` on by default |
| 51 | +
|
| 52 | +[WARNING] `foo` (bin "foo") generated 1 warning |
| 53 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 54 | +
|
| 55 | +"#]]) |
| 56 | + .run(); |
| 57 | +} |
| 58 | + |
| 59 | +#[cargo_test] |
| 60 | +fn rustc_caching_deny_first() { |
| 61 | + let p = make_project_with_rustc_warning(); |
| 62 | + p.cargo("check") |
| 63 | + .with_stderr_data(str![[r#" |
| 64 | +[CHECKING] foo v0.0.1 ([ROOT]/foo) |
| 65 | +[WARNING] unused variable: `x` |
| 66 | + --> src/main.rs:1:17 |
| 67 | + | |
| 68 | +1 | fn main() { let x = 3; } |
| 69 | + | ^ [HELP] if this is intentional, prefix it with an underscore: `_x` |
| 70 | + | |
| 71 | + = [NOTE] `#[warn(unused_variables)]` on by default |
| 72 | +
|
| 73 | +[WARNING] `foo` (bin "foo") generated 1 warning |
| 74 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 75 | +
|
| 76 | +"#]]) |
| 77 | + .run(); |
| 78 | + |
| 79 | + p.cargo("check") |
| 80 | + .with_stderr_data(str![[r#" |
| 81 | +[WARNING] unused variable: `x` |
| 82 | + --> src/main.rs:1:17 |
| 83 | + | |
| 84 | +1 | fn main() { let x = 3; } |
| 85 | + | ^ [HELP] if this is intentional, prefix it with an underscore: `_x` |
| 86 | + | |
| 87 | + = [NOTE] `#[warn(unused_variables)]` on by default |
| 88 | +
|
| 89 | +[WARNING] `foo` (bin "foo") generated 1 warning |
| 90 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 91 | +
|
| 92 | +"#]]) |
| 93 | + .run(); |
| 94 | +} |
| 95 | + |
| 96 | +#[cargo_test] |
| 97 | +fn config() { |
| 98 | + let p = make_project_with_rustc_warning(); |
| 99 | + p.cargo("check") |
| 100 | + .with_stderr_data(str![[r#" |
| 101 | +[CHECKING] foo v0.0.1 ([ROOT]/foo) |
| 102 | +[WARNING] unused variable: `x` |
| 103 | + --> src/main.rs:1:17 |
| 104 | + | |
| 105 | +1 | fn main() { let x = 3; } |
| 106 | + | ^ [HELP] if this is intentional, prefix it with an underscore: `_x` |
| 107 | + | |
| 108 | + = [NOTE] `#[warn(unused_variables)]` on by default |
| 109 | +
|
| 110 | +[WARNING] `foo` (bin "foo") generated 1 warning |
| 111 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 112 | +
|
| 113 | +"#]]) |
| 114 | + .run(); |
| 115 | + |
| 116 | + // CLI has precedence over env |
| 117 | + p.cargo("check") |
| 118 | + .with_stderr_data(str![[r#" |
| 119 | +[WARNING] unused variable: `x` |
| 120 | + --> src/main.rs:1:17 |
| 121 | + | |
| 122 | +1 | fn main() { let x = 3; } |
| 123 | + | ^ [HELP] if this is intentional, prefix it with an underscore: `_x` |
| 124 | + | |
| 125 | + = [NOTE] `#[warn(unused_variables)]` on by default |
| 126 | +
|
| 127 | +[WARNING] `foo` (bin "foo") generated 1 warning |
| 128 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 129 | +
|
| 130 | +"#]]) |
| 131 | + .run(); |
| 132 | +} |
| 133 | + |
| 134 | +#[cargo_test] |
| 135 | +fn requires_nightly() { |
| 136 | + // build.warnings has no effect without -Zwarnings. |
| 137 | + let p = make_project_with_rustc_warning(); |
| 138 | + p.cargo("check") |
| 139 | + .with_stderr_data(str![[r#" |
| 140 | +[CHECKING] foo v0.0.1 ([ROOT]/foo) |
| 141 | +[WARNING] unused variable: `x` |
| 142 | + --> src/main.rs:1:17 |
| 143 | + | |
| 144 | +1 | fn main() { let x = 3; } |
| 145 | + | ^ [HELP] if this is intentional, prefix it with an underscore: `_x` |
| 146 | + | |
| 147 | + = [NOTE] `#[warn(unused_variables)]` on by default |
| 148 | +
|
| 149 | +[WARNING] `foo` (bin "foo") generated 1 warning |
| 150 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 151 | +
|
| 152 | +"#]]) |
| 153 | + .run(); |
| 154 | +} |
| 155 | + |
| 156 | +#[cargo_test] |
| 157 | +fn clippy() { |
| 158 | + let p = project() |
| 159 | + .file( |
| 160 | + "Cargo.toml", |
| 161 | + r#" |
| 162 | + [package] |
| 163 | + name = "foo" |
| 164 | + version = "0.0.1" |
| 165 | + edition = "2015" |
| 166 | + "#, |
| 167 | + ) |
| 168 | + .file("src/lib.rs", "use std::io;") // <-- unused import |
| 169 | + .build(); |
| 170 | + |
| 171 | + p.cargo("check") |
| 172 | + .env("RUSTC_WORKSPACE_WRAPPER", tools::wrapped_clippy_driver()) |
| 173 | + .with_stderr_data(str![[r#" |
| 174 | +[CHECKING] foo v0.0.1 ([ROOT]/foo) |
| 175 | +[WARNING] unused import: `std::io` |
| 176 | +... |
| 177 | +[WARNING] `foo` (lib) generated 1 warning (run `cargo clippy --fix --lib -p foo` to apply 1 suggestion) |
| 178 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 179 | +
|
| 180 | +"#]]) |
| 181 | + .run(); |
| 182 | +} |
0 commit comments