about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-04-15 10:11:46 +0000
committerbors <bors@rust-lang.org>2024-04-15 10:11:46 +0000
commitd493fd148d3033904cd5769dcd8b35b9f2504925 (patch)
tree6bcf724a7c374953c6f0262a1c594b5cedd80273
parent5dcb678ad8dc23a518f9ddf907e6a08de923d05e (diff)
parent8880b702fe06e59fba7fad73481916aa45d81255 (diff)
Auto merge of #115717 - jsgf:stablize-json-unused-externs, r=oli-obk
Stabilize --json unused-externs(-silent)

Implement https://github.com/rust-lang/compiler-team/issues/674 ~~(pending its approval)~~
-rw-r--r--compiler/rustc_session/src/config.rs7
-rw-r--r--src/doc/rustc/src/json.md30
-rw-r--r--tests/ui/unused-crate-deps/deny-cmdline-json-silent.rs2
-rw-r--r--tests/ui/unused-crate-deps/deny-cmdline-json.rs2
-rw-r--r--tests/ui/unused-crate-deps/warn-cmdline-json.rs2
5 files changed, 33 insertions, 10 deletions
diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs
index 34acb4ea10f..7aca86f7169 100644
--- a/compiler/rustc_session/src/config.rs
+++ b/compiler/rustc_session/src/config.rs
@@ -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);
diff --git a/src/doc/rustc/src/json.md b/src/doc/rustc/src/json.md
index 9daa0810126..32083b2f731 100644
--- a/src/doc/rustc/src/json.md
+++ b/src/doc/rustc/src/json.md
@@ -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
diff --git a/tests/ui/unused-crate-deps/deny-cmdline-json-silent.rs b/tests/ui/unused-crate-deps/deny-cmdline-json-silent.rs
index f0f6d5b704d..d8c4382dfc5 100644
--- a/tests/ui/unused-crate-deps/deny-cmdline-json-silent.rs
+++ b/tests/ui/unused-crate-deps/deny-cmdline-json-silent.rs
@@ -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() {}
diff --git a/tests/ui/unused-crate-deps/deny-cmdline-json.rs b/tests/ui/unused-crate-deps/deny-cmdline-json.rs
index aab53eb3775..1f5a2f08e6d 100644
--- a/tests/ui/unused-crate-deps/deny-cmdline-json.rs
+++ b/tests/ui/unused-crate-deps/deny-cmdline-json.rs
@@ -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() {}
diff --git a/tests/ui/unused-crate-deps/warn-cmdline-json.rs b/tests/ui/unused-crate-deps/warn-cmdline-json.rs
index b23a9c8d5fb..68c4d873d67 100644
--- a/tests/ui/unused-crate-deps/warn-cmdline-json.rs
+++ b/tests/ui/unused-crate-deps/warn-cmdline-json.rs
@@ -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() {}