Skip to content

Commit 72da5a9

Browse files
committed
Auto merge of rust-lang#77671 - flip1995:lint_list_always_plugins, r=oli-obk,Manishearth
Always print lints from plugins, if they're available Currently you can get a list of lints and lint groups by running `rustc -Whelp`. This prints an additional line at the end: ``` Compiler plugins can provide additional lints and lint groups. To see a listing of these, re-run `rustc -W help` with a crate filename. ``` Clippy is such a "compiler plugin", that provides additional lints. Running `clippy-driver -Whelp` (`rustc` wrapper) still only prints the rustc lints with the above message at the end. But when running `clippy-driver -Whelp main.rs`, where `main.rs` is any rust file, it also prints Clippy lints. I don't think this is a good approach from a UX perspective: Why is a random file necessary to print a help message? This PR changes this behavior: Whenever a compiler callback registers lints, it is assumed that these lints come from a plugin and are printed without having to specify a Rust source file. Fixes rust-lang/rust-clippy#6122 cc `@Manishearth` `@ebroto` for the Clippy changes.
2 parents 65ecc48 + e54c060 commit 72da5a9

File tree

6 files changed

+14
-3110
lines changed

6 files changed

+14
-3110
lines changed

compiler/rustc_driver/src/lib.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,18 @@ fn run_compiler(
248248
interface::run_compiler(config, |compiler| {
249249
let sopts = &compiler.session().opts;
250250
if sopts.describe_lints {
251-
let lint_store = rustc_lint::new_lint_store(
251+
let mut lint_store = rustc_lint::new_lint_store(
252252
sopts.debugging_opts.no_interleave_lints,
253253
compiler.session().unstable_options(),
254254
);
255-
describe_lints(compiler.session(), &lint_store, false);
255+
let registered_lints =
256+
if let Some(register_lints) = compiler.register_lints() {
257+
register_lints(compiler.session(), &mut lint_store);
258+
true
259+
} else {
260+
false
261+
};
262+
describe_lints(compiler.session(), &lint_store, registered_lints);
256263
return;
257264
}
258265
let should_stop = RustcDefaultCalls::print_crate_info(
@@ -954,10 +961,7 @@ Available lint options:
954961

955962
match (loaded_plugins, plugin.len(), plugin_groups.len()) {
956963
(false, 0, _) | (false, _, 0) => {
957-
println!(
958-
"Compiler plugins can provide additional lints and lint groups. To see a \
959-
listing of these, re-run `rustc -W help` with a crate filename."
960-
);
964+
println!("Lint tools like Clippy can provide additional lints and lint groups.");
961965
}
962966
(false, ..) => panic!("didn't load lint plugins but got them anyway!"),
963967
(true, 0, 0) => println!("This crate does not load any lint plugins or lint groups."),

compiler/rustc_interface/src/interface.rs

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ impl Compiler {
5656
pub fn output_file(&self) -> &Option<PathBuf> {
5757
&self.output_file
5858
}
59+
pub fn register_lints(&self) -> &Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>> {
60+
&self.register_lints
61+
}
5962
pub fn build_output_filenames(
6063
&self,
6164
sess: &Session,

src/tools/clippy/clippy_dev/src/update_lints.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,7 @@ pub fn run(update_mode: UpdateMode) {
2222

2323
let usable_lint_count = round_to_fifty(usable_lints.len());
2424

25-
let mut file_change = replace_region_in_file(
26-
Path::new("src/lintlist/mod.rs"),
27-
"begin lint list",
28-
"end lint list",
29-
false,
30-
update_mode == UpdateMode::Change,
31-
|| {
32-
format!("vec!{:#?}", sorted_usable_lints)
33-
.lines()
34-
.map(ToString::to_string)
35-
.collect::<Vec<_>>()
36-
},
37-
)
38-
.changed;
25+
let mut file_change = false;
3926

4027
file_change |= replace_region_in_file(
4128
Path::new("README.md"),

src/tools/clippy/src/driver.rs

-121
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
// FIXME: switch to something more ergonomic here, once available.
1010
// (Currently there is no way to opt into sysroot crates without `extern crate`.)
11-
extern crate rustc_data_structures;
1211
extern crate rustc_driver;
1312
extern crate rustc_errors;
1413
extern crate rustc_interface;
@@ -26,8 +25,6 @@ use std::panic;
2625
use std::path::{Path, PathBuf};
2726
use std::process::{exit, Command};
2827

29-
mod lintlist;
30-
3128
/// If a command-line option matches `find_arg`, then apply the predicate `pred` on its value. If
3229
/// true, then return it. The parameter is assumed to be either `--arg=value` or `--arg value`.
3330
fn arg_value<'a, T: Deref<Target = str>>(
@@ -92,113 +89,6 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
9289
}
9390
}
9491

95-
#[allow(clippy::find_map, clippy::filter_map)]
96-
fn describe_lints() {
97-
use lintlist::{Level, Lint, ALL_LINTS, LINT_LEVELS};
98-
use rustc_data_structures::fx::FxHashSet;
99-
100-
println!(
101-
"
102-
Available lint options:
103-
-W <foo> Warn about <foo>
104-
-A <foo> Allow <foo>
105-
-D <foo> Deny <foo>
106-
-F <foo> Forbid <foo> (deny <foo> and all attempts to override)
107-
108-
"
109-
);
110-
111-
let lint_level = |lint: &Lint| {
112-
LINT_LEVELS
113-
.iter()
114-
.find(|level_mapping| level_mapping.0 == lint.group)
115-
.map(|(_, level)| match level {
116-
Level::Allow => "allow",
117-
Level::Warn => "warn",
118-
Level::Deny => "deny",
119-
})
120-
.unwrap()
121-
};
122-
123-
let mut lints: Vec<_> = ALL_LINTS.iter().collect();
124-
// The sort doesn't case-fold but it's doubtful we care.
125-
lints.sort_by_cached_key(|x: &&Lint| (lint_level(x), x.name));
126-
127-
let max_lint_name_len = lints
128-
.iter()
129-
.map(|lint| lint.name.len())
130-
.map(|len| len + "clippy::".len())
131-
.max()
132-
.unwrap_or(0);
133-
134-
let padded = |x: &str| {
135-
let mut s = " ".repeat(max_lint_name_len - x.chars().count());
136-
s.push_str(x);
137-
s
138-
};
139-
140-
let scoped = |x: &str| format!("clippy::{}", x);
141-
142-
let lint_groups: FxHashSet<_> = lints.iter().map(|lint| lint.group).collect();
143-
144-
println!("Lint checks provided by clippy:\n");
145-
println!(" {} {:7.7} meaning", padded("name"), "default");
146-
println!(" {} {:7.7} -------", padded("----"), "-------");
147-
148-
let print_lints = |lints: &[&Lint]| {
149-
for lint in lints {
150-
let name = lint.name.replace("_", "-");
151-
println!(
152-
" {} {:7.7} {}",
153-
padded(&scoped(&name)),
154-
lint_level(lint),
155-
lint.desc
156-
);
157-
}
158-
println!("\n");
159-
};
160-
161-
print_lints(&lints);
162-
163-
let max_group_name_len = std::cmp::max(
164-
"clippy::all".len(),
165-
lint_groups
166-
.iter()
167-
.map(|group| group.len())
168-
.map(|len| len + "clippy::".len())
169-
.max()
170-
.unwrap_or(0),
171-
);
172-
173-
let padded_group = |x: &str| {
174-
let mut s = " ".repeat(max_group_name_len - x.chars().count());
175-
s.push_str(x);
176-
s
177-
};
178-
179-
println!("Lint groups provided by clippy:\n");
180-
println!(" {} sub-lints", padded_group("name"));
181-
println!(" {} ---------", padded_group("----"));
182-
println!(" {} the set of all clippy lints", padded_group("clippy::all"));
183-
184-
let print_lint_groups = || {
185-
for group in lint_groups {
186-
let name = group.to_lowercase().replace("_", "-");
187-
let desc = lints
188-
.iter()
189-
.filter(|&lint| lint.group == group)
190-
.map(|lint| lint.name)
191-
.map(|name| name.replace("_", "-"))
192-
.collect::<Vec<String>>()
193-
.join(", ");
194-
println!(" {} {}", padded_group(&scoped(&name)), desc);
195-
}
196-
println!("\n");
197-
};
198-
199-
print_lint_groups();
200-
}
201-
20292
fn display_help() {
20393
println!(
20494
"\
@@ -379,17 +269,6 @@ pub fn main() {
379269
exit(0);
380270
}
381271

382-
let should_describe_lints = || {
383-
let args: Vec<_> = env::args().collect();
384-
args.windows(2)
385-
.any(|args| args[1] == "help" && matches!(args[0].as_str(), "-W" | "-A" | "-D" | "-F"))
386-
};
387-
388-
if !wrapper_mode && should_describe_lints() {
389-
describe_lints();
390-
exit(0);
391-
}
392-
393272
// this conditional check for the --sysroot flag is there so users can call
394273
// `clippy_driver` directly
395274
// without having to pass --sysroot or anything

src/tools/clippy/src/lintlist/lint.rs

-27
This file was deleted.

0 commit comments

Comments
 (0)