-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #72342 - jsgf:warn-unused-deps, r=petrochenkov
Warn about unused crate deps Implements #57274 by adding -Wunused-crate-dependencies. This will warn about any `--extern` option on the command line which isn't referenced by the crate source either via `use` or `extern crate`. Crates which are added for some side effect but are otherwise unreferenced - such as for symbols they define - the warning can be suppressed with `use somecrate as _;`. If a crate has multiple aliases (eg using `foo = { package = "bar" }` in `Cargo.toml`), then it will warn about each unused alias. This does not consider crate added by some other means than `--extern`, including the standard library. It also doesn't consider any crate without `add_prelude` set (though I'm not sure about this). Unfortunately this probably [does not yet work well with Cargo](#57274 (comment)) as it will over-specify crates, causing spurious warnings. As a result, this lint is "allow" by default and must be explicitly enabled either via `#![warn(unused_crate_deps)]` or with `-Wunused-crate-deps`.
- Loading branch information
Showing
17 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub const BAR: &str = "bar"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// edition:2018 | ||
// aux-crate:bar=bar.rs | ||
|
||
pub const FOO: &str = "foo"; | ||
pub use bar::BAR; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Test warnings for a library crate | ||
|
||
// check-pass | ||
// aux-crate:bar=bar.rs | ||
// compile-flags:--crate-type lib -Wunused-crate-dependencies | ||
|
||
pub fn fib(n: u32) -> Vec<u32> { | ||
//~^ WARNING external crate `bar` unused in | ||
let mut prev = 0; | ||
let mut cur = 1; | ||
let mut v = vec![]; | ||
|
||
for _ in 0..n { | ||
v.push(prev); | ||
let n = prev + cur; | ||
prev = cur; | ||
cur = n; | ||
} | ||
|
||
v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
warning: external crate `bar` unused in `libfib`: remove the dependency or add `use bar as _;` | ||
--> $DIR/libfib.rs:7:1 | ||
| | ||
LL | pub fn fib(n: u32) -> Vec<u32> { | ||
| ^ | ||
| | ||
= note: requested on the command line with `-W unused-crate-dependencies` | ||
|
||
warning: 1 warning emitted | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Suppress by using crate | ||
|
||
// edition:2018 | ||
// check-pass | ||
// aux-crate:bar=bar.rs | ||
|
||
#![warn(unused_crate_dependencies)] | ||
|
||
use bar as _; | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Warn about unused aliased for the crate | ||
|
||
// edition:2018 | ||
// check-pass | ||
// aux-crate:bar=bar.rs | ||
// aux-crate:barbar=bar.rs | ||
|
||
#![warn(unused_crate_dependencies)] | ||
//~^ WARNING external crate `barbar` unused in | ||
|
||
use bar as _; | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
warning: external crate `barbar` unused in `unused_aliases`: remove the dependency or add `use barbar as _;` | ||
--> $DIR/unused-aliases.rs:8:1 | ||
| | ||
LL | #![warn(unused_crate_dependencies)] | ||
| ^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/unused-aliases.rs:8:9 | ||
| | ||
LL | #![warn(unused_crate_dependencies)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
warning: 1 warning emitted | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Suppress by using crate | ||
|
||
// edition:2015 | ||
// check-pass | ||
// aux-crate:bar=bar.rs | ||
|
||
#![warn(unused_crate_dependencies)] | ||
|
||
extern crate bar; | ||
|
||
fn main() { | ||
println!("bar {}", bar::BAR); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Check for unused crate dep, no path | ||
|
||
// edition:2018 | ||
// check-pass | ||
// aux-crate:bar=bar.rs | ||
|
||
#![warn(unused_crate_dependencies)] | ||
//~^ WARNING external crate `bar` unused in | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
warning: external crate `bar` unused in `warn_attr`: remove the dependency or add `use bar as _;` | ||
--> $DIR/warn-attr.rs:7:1 | ||
| | ||
LL | #![warn(unused_crate_dependencies)] | ||
| ^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/warn-attr.rs:7:9 | ||
| | ||
LL | #![warn(unused_crate_dependencies)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
warning: 1 warning emitted | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Check for unused crate dep, no path | ||
|
||
// edition:2018 | ||
// check-pass | ||
// compile-flags: -Wunused-crate-dependencies | ||
// aux-crate:bar=bar.rs | ||
// no-prefer-dynamic | ||
|
||
fn main() {} | ||
//~^ WARNING external crate `bar` unused in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
warning: external crate `bar` unused in `warn_cmdline_static`: remove the dependency or add `use bar as _;` | ||
--> $DIR/warn-cmdline-static.rs:9:1 | ||
| | ||
LL | fn main() {} | ||
| ^ | ||
| | ||
= note: requested on the command line with `-W unused-crate-dependencies` | ||
|
||
warning: 1 warning emitted | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Check for unused crate dep, no path | ||
|
||
// edition:2018 | ||
// check-pass | ||
// compile-flags: -Wunused-crate-dependencies | ||
// aux-crate:bar=bar.rs | ||
|
||
fn main() {} | ||
//~^ WARNING external crate `bar` unused in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
warning: external crate `bar` unused in `warn_cmdline`: remove the dependency or add `use bar as _;` | ||
--> $DIR/warn-cmdline.rs:8:1 | ||
| | ||
LL | fn main() {} | ||
| ^ | ||
| | ||
= note: requested on the command line with `-W unused-crate-dependencies` | ||
|
||
warning: 1 warning emitted | ||
|