Skip to content

Commit d493fd1

Browse files
committed
Auto merge of rust-lang#115717 - jsgf:stablize-json-unused-externs, r=oli-obk
Stabilize --json unused-externs(-silent) Implement rust-lang/compiler-team#674 ~~(pending its approval)~~
2 parents 5dcb678 + 8880b70 commit d493fd1

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

compiler/rustc_session/src/config.rs

-7
Original file line numberDiff line numberDiff line change
@@ -2317,13 +2317,6 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
23172317

23182318
check_error_format_stability(early_dcx, &unstable_opts, error_format);
23192319

2320-
if !unstable_opts.unstable_options && json_unused_externs.is_enabled() {
2321-
early_dcx.early_fatal(
2322-
"the `-Z unstable-options` flag must also be passed to enable \
2323-
the flag `--json=unused-externs`",
2324-
);
2325-
}
2326-
23272320
let output_types = parse_output_types(early_dcx, &unstable_opts, matches);
23282321

23292322
let mut cg = CodegenOptions::build(early_dcx, matches);

src/doc/rustc/src/json.md

+30
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,36 @@ information, even if the diagnostics have been suppressed (such as with an
262262
}
263263
```
264264

265+
## Unused Dependency Notifications
266+
267+
The options `--json=unused-externs` and `--json=unused-externs-silent` in
268+
conjunction with the `unused-crate-dependencies` lint will emit JSON structures
269+
reporting any crate dependencies (specified with `--extern`) which never had any
270+
symbols referenced. These are intended to be consumed by the build system which
271+
can then emit diagnostics telling the user to remove the unused dependencies
272+
from `Cargo.toml` (or whatever build-system file defines dependencies).
273+
274+
The JSON structure is:
275+
```json
276+
{
277+
"lint_level": "deny", /* Level of the warning */
278+
"unused_names": [
279+
"foo" /* Names of unused crates, as specified with --extern foo=libfoo.rlib */
280+
],
281+
}
282+
```
283+
284+
The warn/deny/forbid lint level (as defined either on the command line or in the
285+
source) dictates the `lint_level` in the JSON. With `unused-externs`, a
286+
`deny` or `forbid` level diagnostic will also cause `rustc` to exit with a
287+
failure exit code.
288+
289+
`unused-externs-silent` will report the diagnostic the same way, but will not
290+
cause `rustc` to exit with failure - it's up to the consumer to flag failure
291+
appropriately. (This is needed by Cargo which shares the same dependencies
292+
across multiple build targets, so it should only report an unused dependency if
293+
its not used by any of the targets.)
294+
265295
[option-emit]: command-line-arguments.md#option-emit
266296
[option-error-format]: command-line-arguments.md#option-error-format
267297
[option-json]: command-line-arguments.md#option-json

tests/ui/unused-crate-deps/deny-cmdline-json-silent.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//@ edition:2018
44
//@ check-pass
5-
//@ compile-flags: -Dunused-crate-dependencies -Zunstable-options --json unused-externs-silent --error-format=json
5+
//@ compile-flags: -Dunused-crate-dependencies --json unused-externs-silent --error-format=json
66
//@ aux-crate:bar=bar.rs
77

88
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Check for unused crate dep, json event, deny, expect compile failure
22

33
//@ edition:2018
4-
//@ compile-flags: -Dunused-crate-dependencies -Zunstable-options --json unused-externs --error-format=json
4+
//@ compile-flags: -Dunused-crate-dependencies --json unused-externs --error-format=json
55
//@ aux-crate:bar=bar.rs
66

77
fn main() {}

tests/ui/unused-crate-deps/warn-cmdline-json.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//@ edition:2018
44
//@ check-pass
5-
//@ compile-flags: -Wunused-crate-dependencies -Zunstable-options --json unused-externs --error-format=json
5+
//@ compile-flags: -Wunused-crate-dependencies --json unused-externs --error-format=json
66
//@ aux-crate:bar=bar.rs
77

88
fn main() {}

0 commit comments

Comments
 (0)