Skip to content

Commit 21123dd

Browse files
authored
Rollup merge of rust-lang#139650 - Alexendoo:group-alias, r=davidtwco
Fix `register_group_alias` for tools In clippy we're looking at renaming `clippy::all` and registering an alias for it but currently that doesn't work for tools The `lint_ids` of the alias are now populated at the time of registration to make it easier to handle
2 parents 8150743 + 3375264 commit 21123dd

File tree

3 files changed

+42
-63
lines changed

3 files changed

+42
-63
lines changed

Diff for: compiler/rustc_lint/src/context.rs

+39-58
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ enum TargetLint {
8383
Ignored,
8484
}
8585

86-
pub enum FindLintError {
87-
NotFound,
88-
Removed,
89-
}
90-
9186
struct LintAlias {
9287
name: &'static str,
9388
/// Whether deprecation warnings should be suppressed for this alias.
@@ -231,13 +226,24 @@ impl LintStore {
231226
}
232227
}
233228

234-
pub fn register_group_alias(&mut self, lint_name: &'static str, alias: &'static str) {
235-
self.lint_groups.insert(
229+
fn insert_group(&mut self, name: &'static str, group: LintGroup) {
230+
let previous = self.lint_groups.insert(name, group);
231+
if previous.is_some() {
232+
bug!("group {name:?} already exists");
233+
}
234+
}
235+
236+
pub fn register_group_alias(&mut self, group_name: &'static str, alias: &'static str) {
237+
let Some(LintGroup { lint_ids, .. }) = self.lint_groups.get(group_name) else {
238+
bug!("group alias {alias:?} points to unregistered group {group_name:?}")
239+
};
240+
241+
self.insert_group(
236242
alias,
237243
LintGroup {
238-
lint_ids: vec![],
244+
lint_ids: lint_ids.clone(),
239245
is_externally_loaded: false,
240-
depr: Some(LintAlias { name: lint_name, silent: true }),
246+
depr: Some(LintAlias { name: group_name, silent: true }),
241247
},
242248
);
243249
}
@@ -249,24 +255,17 @@ impl LintStore {
249255
deprecated_name: Option<&'static str>,
250256
to: Vec<LintId>,
251257
) {
252-
let new = self
253-
.lint_groups
254-
.insert(name, LintGroup { lint_ids: to, is_externally_loaded, depr: None })
255-
.is_none();
256258
if let Some(deprecated) = deprecated_name {
257-
self.lint_groups.insert(
259+
self.insert_group(
258260
deprecated,
259261
LintGroup {
260-
lint_ids: vec![],
262+
lint_ids: to.clone(),
261263
is_externally_loaded,
262264
depr: Some(LintAlias { name, silent: false }),
263265
},
264266
);
265267
}
266-
267-
if !new {
268-
bug!("duplicate specification of lint group {}", name);
269-
}
268+
self.insert_group(name, LintGroup { lint_ids: to, is_externally_loaded, depr: None });
270269
}
271270

272271
/// This lint should give no warning and have no effect.
@@ -292,23 +291,15 @@ impl LintStore {
292291
self.by_name.insert(name.into(), Removed(reason.into()));
293292
}
294293

295-
pub fn find_lints(&self, mut lint_name: &str) -> Result<Vec<LintId>, FindLintError> {
294+
pub fn find_lints(&self, lint_name: &str) -> Option<&[LintId]> {
296295
match self.by_name.get(lint_name) {
297-
Some(&Id(lint_id)) => Ok(vec![lint_id]),
298-
Some(&Renamed(_, lint_id)) => Ok(vec![lint_id]),
299-
Some(&Removed(_)) => Err(FindLintError::Removed),
300-
Some(&Ignored) => Ok(vec![]),
301-
None => loop {
302-
return match self.lint_groups.get(lint_name) {
303-
Some(LintGroup { lint_ids, depr, .. }) => {
304-
if let Some(LintAlias { name, .. }) = depr {
305-
lint_name = name;
306-
continue;
307-
}
308-
Ok(lint_ids.clone())
309-
}
310-
None => Err(FindLintError::Removed),
311-
};
296+
Some(Id(lint_id)) => Some(slice::from_ref(lint_id)),
297+
Some(Renamed(_, lint_id)) => Some(slice::from_ref(lint_id)),
298+
Some(Removed(_)) => None,
299+
Some(Ignored) => Some(&[]),
300+
None => match self.lint_groups.get(lint_name) {
301+
Some(LintGroup { lint_ids, .. }) => Some(lint_ids),
302+
None => None,
312303
},
313304
}
314305
}
@@ -374,8 +365,12 @@ impl LintStore {
374365
CheckLintNameResult::MissingTool
375366
};
376367
}
377-
Some(LintGroup { lint_ids, .. }) => {
378-
return CheckLintNameResult::Tool(lint_ids, None);
368+
Some(LintGroup { lint_ids, depr, .. }) => {
369+
return if let &Some(LintAlias { name, silent: false }) = depr {
370+
CheckLintNameResult::Tool(lint_ids, Some(name.to_string()))
371+
} else {
372+
CheckLintNameResult::Tool(lint_ids, None)
373+
};
379374
}
380375
},
381376
Some(Id(id)) => return CheckLintNameResult::Tool(slice::from_ref(id), None),
@@ -393,15 +388,11 @@ impl LintStore {
393388
None => self.check_tool_name_for_backwards_compat(&complete_name, "clippy"),
394389
Some(LintGroup { lint_ids, depr, .. }) => {
395390
// Check if the lint group name is deprecated
396-
if let Some(LintAlias { name, silent }) = depr {
397-
let LintGroup { lint_ids, .. } = self.lint_groups.get(name).unwrap();
398-
return if *silent {
399-
CheckLintNameResult::Ok(lint_ids)
400-
} else {
401-
CheckLintNameResult::Tool(lint_ids, Some((*name).to_string()))
402-
};
391+
if let &Some(LintAlias { name, silent: false }) = depr {
392+
CheckLintNameResult::Tool(lint_ids, Some(name.to_string()))
393+
} else {
394+
CheckLintNameResult::Ok(lint_ids)
403395
}
404-
CheckLintNameResult::Ok(lint_ids)
405396
}
406397
},
407398
Some(Id(id)) => CheckLintNameResult::Ok(slice::from_ref(id)),
@@ -412,7 +403,7 @@ impl LintStore {
412403
fn no_lint_suggestion(&self, lint_name: &str, tool_name: &str) -> CheckLintNameResult<'_> {
413404
let name_lower = lint_name.to_lowercase();
414405

415-
if lint_name.chars().any(char::is_uppercase) && self.find_lints(&name_lower).is_ok() {
406+
if lint_name.chars().any(char::is_uppercase) && self.find_lints(&name_lower).is_some() {
416407
// First check if the lint name is (partly) in upper case instead of lower case...
417408
return CheckLintNameResult::NoLint(Some((Symbol::intern(&name_lower), false)));
418409
}
@@ -455,18 +446,8 @@ impl LintStore {
455446
None => match self.lint_groups.get(&*complete_name) {
456447
// Now we are sure, that this lint exists nowhere
457448
None => self.no_lint_suggestion(lint_name, tool_name),
458-
Some(LintGroup { lint_ids, depr, .. }) => {
459-
// Reaching this would be weird, but let's cover this case anyway
460-
if let Some(LintAlias { name, silent }) = depr {
461-
let LintGroup { lint_ids, .. } = self.lint_groups.get(name).unwrap();
462-
if *silent {
463-
CheckLintNameResult::Tool(lint_ids, Some(complete_name))
464-
} else {
465-
CheckLintNameResult::Tool(lint_ids, Some((*name).to_string()))
466-
}
467-
} else {
468-
CheckLintNameResult::Tool(lint_ids, Some(complete_name))
469-
}
449+
Some(LintGroup { lint_ids, .. }) => {
450+
CheckLintNameResult::Tool(lint_ids, Some(complete_name))
470451
}
471452
},
472453
Some(Id(id)) => CheckLintNameResult::Tool(slice::from_ref(id), Some(complete_name)),

Diff for: compiler/rustc_lint/src/levels.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,11 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
517517

518518
let lint_flag_val = Symbol::intern(lint_name);
519519

520-
let Ok(ids) = self.store.find_lints(lint_name) else {
520+
let Some(ids) = self.store.find_lints(lint_name) else {
521521
// errors already handled above
522522
continue;
523523
};
524-
for id in ids {
524+
for &id in ids {
525525
// ForceWarn and Forbid cannot be overridden
526526
if let Some(LevelAndSource { level: Level::ForceWarn | Level::Forbid, .. }) =
527527
self.current_specs().get(&id)

Diff for: compiler/rustc_lint/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ use unused::*;
124124

125125
#[rustfmt::skip]
126126
pub use builtin::{MissingDoc, SoftLints};
127-
pub use context::{
128-
CheckLintNameResult, EarlyContext, FindLintError, LateContext, LintContext, LintStore,
129-
};
127+
pub use context::{CheckLintNameResult, EarlyContext, LateContext, LintContext, LintStore};
130128
pub use early::{EarlyCheckNode, check_ast_node};
131129
pub use late::{check_crate, late_lint_mod, unerased_lint_store};
132130
pub use levels::LintLevelsBuilder;

0 commit comments

Comments
 (0)