Skip to content

Commit 19ff2e0

Browse files
committed
rough solution
1 parent d41e12f commit 19ff2e0

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
@@ -62,7 +62,11 @@ pub struct LintStore {
6262
lint_groups: FxIndexMap<&'static str, LintGroup>,
6363
}
6464

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

6771
/// The target of the `by_name` map, which accounts for renaming/deprecation.
6872
#[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();
@@ -427,7 +444,7 @@ pub fn lint_level(
427444
decorate(&mut err);
428445
}
429446

430-
explain_lint_level_source(lint, level, src, &mut err);
447+
explain_lint_level_source(sess, lint, level, src, &mut err);
431448
err.emit()
432449
}
433450
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
@@ -44,6 +44,7 @@ use crate::config::{
4444
SwitchWithOptPath,
4545
};
4646
use crate::filesearch::FileSearch;
47+
use crate::lint::LintId;
4748
use crate::parse::{ParseSess, add_feature_diagnostics};
4849
use crate::search_paths::SearchPath;
4950
use crate::{errors, filesearch, lint};
@@ -139,7 +140,9 @@ pub struct CompilerIO {
139140
pub temps_dir: Option<PathBuf>,
140141
}
141142

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

144147
/// Represents the data associated with a compilation
145148
/// session for a single crate.
@@ -603,6 +606,13 @@ impl Session {
603606
(&*self.target.staticlib_prefix, &*self.target.staticlib_suffix)
604607
}
605608
}
609+
610+
pub fn lint_groups(&self) -> Box<dyn Iterator<Item = (&'static str, Vec<LintId>, bool)> + '_> {
611+
match self.lint_store {
612+
Some(ref lint_store) => lint_store.lint_groups(),
613+
None => Box::new(std::iter::empty()),
614+
}
615+
}
606616
}
607617

608618
// JUSTIFICATION: defn of the suggested wrapper fns

0 commit comments

Comments
 (0)