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-deps` or `#![warn(unused_crate_deps)]` 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 _;`. The diagnostics will name the crate and the source file of its root module. Ideally, however, it would point to the place where the dependency is defined (in Cargo.toml, for example) to allow the problem to be solved directly. That still needs some design work and will be in a later PR. Resolves issue rust-lang#57274
- Loading branch information
1 parent
75b0a68
commit 65b5619
Showing
23 changed files
with
318 additions
and
12 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
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,19 @@ | ||
// check-pass | ||
// aux-crate:bar=bar.rs | ||
// compile-flags:--crate-type lib -Wunused-crate-deps | ||
|
||
pub fn fib(n: u32) -> Vec<u32> { | ||
//~^ External crate `bar` unused in `libfib`. Remove the dependency or add `use bar as _;`. | ||
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:5:1 | ||
| | ||
LL | pub fn fib(n: u32) -> Vec<u32> { | ||
| ^ | ||
| | ||
= note: requested on the command line with `-W unused-crate-deps` | ||
|
||
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_deps)] | ||
|
||
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 @@ | ||
// Warn about unused aliased for the crate | ||
|
||
// edition:2018 | ||
// check-pass | ||
// aux-crate:bar=bar.rs | ||
// aux-crate:barbar=bar.rs | ||
// ignore-tidy-linelength | ||
|
||
#![warn(unused_crate_deps)] | ||
//~^ WARNING External crate `barbar` unused in `unused_aliases_dylib`. Remove the dependency or add `use barbar as _;`. | ||
|
||
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_dylib`. Remove the dependency or add `use barbar as _;`. | ||
--> $DIR/unused-aliases-dylib.rs:9:1 | ||
| | ||
LL | #![warn(unused_crate_deps)] | ||
| ^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/unused-aliases-dylib.rs:9:9 | ||
| | ||
LL | #![warn(unused_crate_deps)] | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
||
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,15 @@ | ||
// Warn about unused aliased for the crate | ||
|
||
// edition:2018 | ||
// check-pass | ||
// aux-crate:bar=bar.rs | ||
// aux-crate:barbar=bar.rs | ||
// no-prefer-dynamic | ||
// ignore-tidy-linelength | ||
|
||
#![warn(unused_crate_deps)] | ||
//~^ WARNING External crate `barbar` unused in `unused_aliases`. Remove the dependency or add `use barbar as _;`. | ||
|
||
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:10:1 | ||
| | ||
LL | #![warn(unused_crate_deps)] | ||
| ^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/unused-aliases.rs:10:9 | ||
| | ||
LL | #![warn(unused_crate_deps)] | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
||
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_deps)] | ||
|
||
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,11 @@ | ||
// Check for unused crate dep, no path | ||
|
||
// edition:2018 | ||
// check-pass | ||
// aux-crate:bar=bar.rs | ||
// ignore-tidy-linelength | ||
|
||
#![warn(unused_crate_deps)] | ||
//~^ WARNING External crate `bar` unused in `warn_attr_dylib`. Remove the dependency or add `use bar as _;`. | ||
|
||
fn main() {} |
Oops, something went wrong.