Skip to content

Commit 8a7048b

Browse files
committed
Auto merge of #54251 - varkor:silence-bad_style, r=Manishearth
Make `bad_style` a silent alias for `nonstandard_style` Now only `nonstandard_style` is suggested in `rustc -W help`, but `bad_style` will not produce a warning. Closes #41646. r? @Manishearth
2 parents 99ab2f4 + 15ecd19 commit 8a7048b

File tree

2 files changed

+94
-45
lines changed

2 files changed

+94
-45
lines changed

src/librustc/lint/context.rs

Lines changed: 85 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@ pub struct LintStore {
6767
/// Lints indexed by name.
6868
by_name: FxHashMap<String, TargetLint>,
6969

70-
/// Map of registered lint groups to what lints they expand to. The first
71-
/// bool is true if the lint group was added by a plugin. The optional string
72-
/// is used to store the new names of deprecated lint group names.
73-
lint_groups: FxHashMap<&'static str, (Vec<LintId>, bool, Option<&'static str>)>,
70+
/// Map of registered lint groups to what lints they expand to.
71+
lint_groups: FxHashMap<&'static str, LintGroup>,
7472

7573
/// Extra info for future incompatibility lints, describing the
7674
/// issue or RFC that caused the incompatibility.
@@ -127,6 +125,18 @@ pub enum FindLintError {
127125
Removed,
128126
}
129127

128+
struct LintAlias {
129+
name: &'static str,
130+
/// Whether deprecation warnings should be suppressed for this alias.
131+
silent: bool,
132+
}
133+
134+
struct LintGroup {
135+
lint_ids: Vec<LintId>,
136+
from_plugin: bool,
137+
depr: Option<LintAlias>,
138+
}
139+
130140
pub enum CheckLintNameResult<'a> {
131141
Ok(&'a [LintId]),
132142
/// Lint doesn't exist
@@ -160,9 +170,15 @@ impl LintStore {
160170
}
161171

162172
pub fn get_lint_groups<'t>(&'t self) -> Vec<(&'static str, Vec<LintId>, bool)> {
163-
self.lint_groups.iter().map(|(k, v)| (*k,
164-
v.0.clone(),
165-
v.1)).collect()
173+
self.lint_groups.iter()
174+
.filter(|(_, LintGroup { depr, .. })| {
175+
// Don't display deprecated lint groups.
176+
depr.is_none()
177+
})
178+
.map(|(k, LintGroup { lint_ids, from_plugin, .. })| {
179+
(*k, lint_ids.clone(), *from_plugin)
180+
})
181+
.collect()
166182
}
167183

168184
pub fn register_early_pass(&mut self,
@@ -245,6 +261,18 @@ impl LintStore {
245261
self.future_incompatible.get(&id)
246262
}
247263

264+
pub fn register_group_alias(
265+
&mut self,
266+
lint_name: &'static str,
267+
alias: &'static str,
268+
) {
269+
self.lint_groups.insert(alias, LintGroup {
270+
lint_ids: vec![],
271+
from_plugin: false,
272+
depr: Some(LintAlias { name: lint_name, silent: true }),
273+
});
274+
}
275+
248276
pub fn register_group(
249277
&mut self,
250278
sess: Option<&Session>,
@@ -255,11 +283,18 @@ impl LintStore {
255283
) {
256284
let new = self
257285
.lint_groups
258-
.insert(name, (to, from_plugin, None))
286+
.insert(name, LintGroup {
287+
lint_ids: to,
288+
from_plugin,
289+
depr: None,
290+
})
259291
.is_none();
260292
if let Some(deprecated) = deprecated_name {
261-
self.lint_groups
262-
.insert(deprecated, (vec![], from_plugin, Some(name)));
293+
self.lint_groups.insert(deprecated, LintGroup {
294+
lint_ids: vec![],
295+
from_plugin,
296+
depr: Some(LintAlias { name, silent: false }),
297+
});
263298
}
264299

265300
if !new {
@@ -288,7 +323,7 @@ impl LintStore {
288323
self.by_name.insert(name.into(), Removed(reason.into()));
289324
}
290325

291-
pub fn find_lints(&self, lint_name: &str) -> Result<Vec<LintId>, FindLintError> {
326+
pub fn find_lints(&self, mut lint_name: &str) -> Result<Vec<LintId>, FindLintError> {
292327
match self.by_name.get(lint_name) {
293328
Some(&Id(lint_id)) => Ok(vec![lint_id]),
294329
Some(&Renamed(_, lint_id)) => {
@@ -298,9 +333,17 @@ impl LintStore {
298333
Err(FindLintError::Removed)
299334
},
300335
None => {
301-
match self.lint_groups.get(lint_name) {
302-
Some(v) => Ok(v.0.clone()),
303-
None => Err(FindLintError::Removed)
336+
loop {
337+
return match self.lint_groups.get(lint_name) {
338+
Some(LintGroup {lint_ids, depr, .. }) => {
339+
if let Some(LintAlias { name, .. }) = depr {
340+
lint_name = name;
341+
continue;
342+
}
343+
Ok(lint_ids.clone())
344+
}
345+
None => Err(FindLintError::Removed)
346+
};
304347
}
305348
}
306349
}
@@ -366,7 +409,9 @@ impl LintStore {
366409
match self.by_name.get(&complete_name) {
367410
None => match self.lint_groups.get(&*complete_name) {
368411
None => return CheckLintNameResult::Tool(Err((None, String::new()))),
369-
Some(ids) => return CheckLintNameResult::Tool(Ok(&ids.0)),
412+
Some(LintGroup { lint_ids, .. }) => {
413+
return CheckLintNameResult::Tool(Ok(&lint_ids));
414+
}
370415
},
371416
Some(&Id(ref id)) => return CheckLintNameResult::Tool(Ok(slice::from_ref(id))),
372417
// If the lint was registered as removed or renamed by the lint tool, we don't need
@@ -390,16 +435,20 @@ impl LintStore {
390435
// If neither the lint, nor the lint group exists check if there is a `clippy::`
391436
// variant of this lint
392437
None => self.check_tool_name_for_backwards_compat(&complete_name, "clippy"),
393-
Some(ids) => {
438+
Some(LintGroup { lint_ids, depr, .. }) => {
394439
// Check if the lint group name is deprecated
395-
if let Some(new_name) = ids.2 {
396-
let lint_ids = self.lint_groups.get(new_name).unwrap();
397-
return CheckLintNameResult::Tool(Err((
398-
Some(&lint_ids.0),
399-
new_name.to_string(),
400-
)));
440+
if let Some(LintAlias { name, silent }) = depr {
441+
let LintGroup { lint_ids, .. } = self.lint_groups.get(name).unwrap();
442+
return if *silent {
443+
CheckLintNameResult::Ok(&lint_ids)
444+
} else {
445+
CheckLintNameResult::Tool(Err((
446+
Some(&lint_ids),
447+
name.to_string(),
448+
)))
449+
};
401450
}
402-
CheckLintNameResult::Ok(&ids.0)
451+
CheckLintNameResult::Ok(&lint_ids)
403452
}
404453
},
405454
Some(&Id(ref id)) => CheckLintNameResult::Ok(slice::from_ref(id)),
@@ -416,16 +465,20 @@ impl LintStore {
416465
None => match self.lint_groups.get(&*complete_name) {
417466
// Now we are sure, that this lint exists nowhere
418467
None => CheckLintNameResult::NoLint,
419-
Some(ids) => {
420-
// Reaching this would be weird, but lets cover this case anyway
421-
if let Some(new_name) = ids.2 {
422-
let lint_ids = self.lint_groups.get(new_name).unwrap();
423-
return CheckLintNameResult::Tool(Err((
424-
Some(&lint_ids.0),
425-
new_name.to_string(),
426-
)));
468+
Some(LintGroup { lint_ids, depr, .. }) => {
469+
// Reaching this would be weird, but let's cover this case anyway
470+
if let Some(LintAlias { name, silent }) = depr {
471+
let LintGroup { lint_ids, .. } = self.lint_groups.get(name).unwrap();
472+
return if *silent {
473+
CheckLintNameResult::Tool(Err((Some(&lint_ids), complete_name)))
474+
} else {
475+
CheckLintNameResult::Tool(Err((
476+
Some(&lint_ids),
477+
name.to_string(),
478+
)))
479+
};
427480
}
428-
CheckLintNameResult::Tool(Err((Some(&ids.0), complete_name)))
481+
CheckLintNameResult::Tool(Err((Some(&lint_ids), complete_name)))
429482
}
430483
},
431484
Some(&Id(ref id)) => {

src/librustc_lint/lib.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,30 +84,30 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
8484
($sess:ident, $($name:ident),*,) => (
8585
{$(
8686
store.register_early_pass($sess, false, box $name);
87-
)*}
88-
)
87+
)*}
88+
)
8989
}
9090

9191
macro_rules! add_pre_expansion_builtin {
9292
($sess:ident, $($name:ident),*,) => (
9393
{$(
9494
store.register_pre_expansion_pass($sess, box $name);
95-
)*}
96-
)
95+
)*}
96+
)
9797
}
9898

9999
macro_rules! add_early_builtin_with_new {
100100
($sess:ident, $($name:ident),*,) => (
101101
{$(
102102
store.register_early_pass($sess, false, box $name::new());
103-
)*}
104-
)
103+
)*}
104+
)
105105
}
106106

107107
macro_rules! add_lint_group {
108108
($sess:ident, $name:expr, $($lint:ident),*) => (
109109
store.register_group($sess, false, $name, None, vec![$(LintId::of($lint)),*]);
110-
)
110+
)
111111
}
112112

113113
add_pre_expansion_builtin!(sess,
@@ -162,12 +162,6 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
162162

163163
store.register_late_pass(sess, false, box BuiltinCombinedLateLintPass::new());
164164

165-
add_lint_group!(sess,
166-
"bad_style",
167-
NON_CAMEL_CASE_TYPES,
168-
NON_SNAKE_CASE,
169-
NON_UPPER_CASE_GLOBALS);
170-
171165
add_lint_group!(sess,
172166
"nonstandard_style",
173167
NON_CAMEL_CASE_TYPES,
@@ -357,6 +351,8 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
357351
store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
358352
store.register_removed("negate_unsigned", "cast a signed value instead");
359353
store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
354+
// Register lint group aliases
355+
store.register_group_alias("nonstandard_style", "bad_style");
360356
// This was renamed to raw_pointer_derive, which was then removed,
361357
// so it is also considered removed
362358
store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");

0 commit comments

Comments
 (0)