Skip to content

Commit d49983f

Browse files
committed
rough solution
1 parent 7e552b4 commit d49983f

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

compiler/rustc_lint/src/context.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ pub struct LintStore {
6161
lint_groups: FxIndexMap<&'static str, LintGroup>,
6262
}
6363

64-
impl LintStoreMarker for LintStore {}
64+
impl LintStoreMarker for LintStore {
65+
fn lint_groups(&self) -> Box<dyn Iterator<Item = (&'static str, Vec<LintId>, bool)> + '_> {
66+
Box::new(self.get_lint_groups())
67+
}
68+
}
6569

6670
/// The target of the `by_name` map, which accounts for renaming/deprecation.
6771
#[derive(Debug)]

compiler/rustc_middle/src/lint.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,22 @@ impl LintExpectation {
211211
}
212212

213213
fn explain_lint_level_source(
214+
sess: &Session,
214215
lint: &'static Lint,
215216
level: Level,
216217
src: LintLevelSource,
217218
err: &mut Diag<'_, ()>,
218219
) {
220+
fn lint_group_name(lint: &'static Lint, sess: &Session) -> Option<&'static str> {
221+
let mut lint_groups_iter = sess.lint_groups();
222+
let lint_id = LintId::of(lint);
223+
lint_groups_iter
224+
.find(|lint_group| {
225+
let lints = &lint_group.1;
226+
lints.iter().find(|lint_group_lint| **lint_group_lint == lint_id).is_some()
227+
})
228+
.map(|lint_group| lint_group.0)
229+
}
219230
let name = lint.name_lower();
220231
if let Level::Allow = level {
221232
// Do not point at `#[allow(compat_lint)]` as the reason for a compatibility lint
@@ -224,7 +235,13 @@ fn explain_lint_level_source(
224235
}
225236
match src {
226237
LintLevelSource::Default => {
227-
err.note_once(format!("`#[{}({})]` on by default", level.as_str(), name));
238+
let level_str = level.as_str();
239+
err.note_once(format!("`#[{level_str}({name})]` on by default"));
240+
if let Some(group_name) = lint_group_name(lint, sess) {
241+
err.note_once(format!(
242+
"`#[{level_str}({name})]` implied by `#[{level_str}({group_name})]`"
243+
));
244+
}
228245
}
229246
LintLevelSource::CommandLine(lint_flag_val, orig_level) => {
230247
let flag = orig_level.to_cmd_flag();
@@ -428,7 +445,7 @@ pub fn lint_level(
428445
decorate(&mut err);
429446
}
430447

431-
explain_lint_level_source(lint, level, src, &mut err);
448+
explain_lint_level_source(sess, lint, level, src, &mut err);
432449
err.emit()
433450
}
434451
lint_level_impl(sess, lint, level, span, Box::new(decorate))

compiler/rustc_session/src/session.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use crate::config::{
4242
SwitchWithOptPath,
4343
};
4444
use crate::filesearch::FileSearch;
45+
use crate::lint::LintId;
4546
use crate::parse::{ParseSess, add_feature_diagnostics};
4647
use crate::search_paths::SearchPath;
4748
use crate::{errors, filesearch, lint};
@@ -137,7 +138,9 @@ pub struct CompilerIO {
137138
pub temps_dir: Option<PathBuf>,
138139
}
139140

140-
pub trait LintStoreMarker: Any + DynSync + DynSend {}
141+
pub trait LintStoreMarker: Any + DynSync + DynSend {
142+
fn lint_groups(&self) -> Box<dyn Iterator<Item = (&'static str, Vec<LintId>, bool)> + '_>;
143+
}
141144

142145
/// Represents the data associated with a compilation
143146
/// session for a single crate.
@@ -608,6 +611,13 @@ impl Session {
608611
(&*self.target.staticlib_prefix, &*self.target.staticlib_suffix)
609612
}
610613
}
614+
615+
pub fn lint_groups(&self) -> Box<dyn Iterator<Item = (&'static str, Vec<LintId>, bool)> + '_> {
616+
match self.lint_store {
617+
Some(ref lint_store) => lint_store.lint_groups(),
618+
None => Box::new(std::iter::empty()),
619+
}
620+
}
611621
}
612622

613623
// JUSTIFICATION: defn of the suggested wrapper fns

0 commit comments

Comments
 (0)