Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize --json unused-externs(-silent) #115717

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2317,13 +2317,6 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M

check_error_format_stability(early_dcx, &unstable_opts, error_format);

if !unstable_opts.unstable_options && json_unused_externs.is_enabled() {
early_dcx.early_fatal(
"the `-Z unstable-options` flag must also be passed to enable \
the flag `--json=unused-externs`",
);
}

let output_types = parse_output_types(early_dcx, &unstable_opts, matches);

let mut cg = CodegenOptions::build(early_dcx, matches);
Expand Down
30 changes: 30 additions & 0 deletions src/doc/rustc/src/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,36 @@ information, even if the diagnostics have been suppressed (such as with an
}
```

## Unused Dependency Notifications

The options `--json=unused-externs` and `--json=unused-externs-silent` in
conjunction with the `unused-crate-dependencies` lint will emit JSON structures
reporting any crate dependencies (specified with `--extern`) which never had any
symbols referenced. These are intended to be consumed by the build system which
can then emit diagnostics telling the user to remove the unused dependencies
from `Cargo.toml` (or whatever build-system file defines dependencies).

The JSON structure is:
```json
{
"lint_level": "deny", /* Level of the warning */
"unused_names": [
"foo" /* Names of unused crates, as specified with --extern foo=libfoo.rlib */
],
}
```

The warn/deny/forbid lint level (as defined either on the command line or in the
source) dictates the `lint_level` in the JSON. With `unused-externs`, a
`deny` or `forbid` level diagnostic will also cause `rustc` to exit with a
failure exit code.

`unused-externs-silent` will report the diagnostic the same way, but will not
cause `rustc` to exit with failure - it's up to the consumer to flag failure
appropriately. (This is needed by Cargo which shares the same dependencies
across multiple build targets, so it should only report an unused dependency if
its not used by any of the targets.)

[option-emit]: command-line-arguments.md#option-emit
[option-error-format]: command-line-arguments.md#option-error-format
[option-json]: command-line-arguments.md#option-json
2 changes: 1 addition & 1 deletion tests/ui/unused-crate-deps/deny-cmdline-json-silent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/unused-crate-deps/deny-cmdline-json.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Check for unused crate dep, json event, deny, expect compile failure

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

fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/unused-crate-deps/warn-cmdline-json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

fn main() {}
Loading