forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement warning for unused dependencies.
This will print a diagnostic for crates which are mentioned as `--extern` arguments on the command line, but are never referenced from the source. This diagnostic is controlled by `-Wunused-crate-dependencies` or `#![warn(unused_crate_dependencies)]` and is "allow" by default. There are cases where certain crates need to be linked in but are not directly referenced - for example if they are providing symbols for C linkage. In this case the warning can be suppressed with `use needed_crate as _;`. Thanks to @petrochenkov for simplified core. Resolves issue rust-lang#57274
- 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 | ||
|