Skip to content

Commit 9f2d0b3

Browse files
committed
Auto merge of #125057 - nnethercote:rustc_lint-cleanups, r=cjgillot
`rustc_lint` cleanups Minor improvements I found while looking through this code. r? `@cjgillot`
2 parents 032af18 + 32c8a12 commit 9f2d0b3

11 files changed

+117
-106
lines changed

compiler/rustc_lint/src/builtin.rs

+26-16
Original file line numberDiff line numberDiff line change
@@ -1494,8 +1494,9 @@ impl<'tcx> LateLintPass<'tcx> for TypeAliasBounds {
14941494

14951495
let ty = cx.tcx.type_of(item.owner_id).skip_binder();
14961496
if ty.has_inherent_projections() {
1497-
// Bounds of type aliases that contain opaque types or inherent projections are respected.
1498-
// E.g: `type X = impl Trait;`, `type X = (impl Trait, Y);`, `type X = Type::Inherent;`.
1497+
// Bounds of type aliases that contain opaque types or inherent projections are
1498+
// respected. E.g: `type X = impl Trait;`, `type X = (impl Trait, Y);`, `type X =
1499+
// Type::Inherent;`.
14991500
return;
15001501
}
15011502

@@ -2224,7 +2225,8 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
22242225
hir_generics.span.shrink_to_hi().to(where_span)
22252226
};
22262227

2227-
// Due to macro expansions, the `full_where_span` might not actually contain all predicates.
2228+
// Due to macro expansions, the `full_where_span` might not actually contain all
2229+
// predicates.
22282230
if where_lint_spans.iter().all(|&sp| full_where_span.contains(sp)) {
22292231
lint_spans.push(full_where_span);
22302232
} else {
@@ -2601,7 +2603,8 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
26012603
};
26022604
// So we have at least one potentially inhabited variant. Might we have two?
26032605
let Some(second_variant) = potential_variants.next() else {
2604-
// There is only one potentially inhabited variant. So we can recursively check that variant!
2606+
// There is only one potentially inhabited variant. So we can recursively
2607+
// check that variant!
26052608
return variant_find_init_error(
26062609
cx,
26072610
ty,
@@ -2611,10 +2614,10 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
26112614
init,
26122615
);
26132616
};
2614-
// So we have at least two potentially inhabited variants.
2615-
// If we can prove that we have at least two *definitely* inhabited variants,
2616-
// then we have a tag and hence leaving this uninit is definitely disallowed.
2617-
// (Leaving it zeroed could be okay, depending on which variant is encoded as zero tag.)
2617+
// So we have at least two potentially inhabited variants. If we can prove that
2618+
// we have at least two *definitely* inhabited variants, then we have a tag and
2619+
// hence leaving this uninit is definitely disallowed. (Leaving it zeroed could
2620+
// be okay, depending on which variant is encoded as zero tag.)
26182621
if init == InitKind::Uninit {
26192622
let definitely_inhabited = (first_variant.1 as usize)
26202623
+ (second_variant.1 as usize)
@@ -2825,7 +2828,8 @@ impl<'tcx> LateLintPass<'tcx> for NamedAsmLabels {
28252828

28262829
let mut found_labels = Vec::new();
28272830

2828-
// A semicolon might not actually be specified as a separator for all targets, but it seems like LLVM accepts it always
2831+
// A semicolon might not actually be specified as a separator for all targets, but
2832+
// it seems like LLVM accepts it always.
28292833
let statements = template_str.split(|c| matches!(c, '\n' | ';'));
28302834
for statement in statements {
28312835
// If there's a comment, trim it from the statement
@@ -2838,7 +2842,8 @@ impl<'tcx> LateLintPass<'tcx> for NamedAsmLabels {
28382842
let mut chars = possible_label.chars();
28392843

28402844
let Some(start) = chars.next() else {
2841-
// Empty string means a leading ':' in this section, which is not a label.
2845+
// Empty string means a leading ':' in this section, which is not a
2846+
// label.
28422847
break 'label_loop;
28432848
};
28442849

@@ -2855,12 +2860,15 @@ impl<'tcx> LateLintPass<'tcx> for NamedAsmLabels {
28552860

28562861
// Labels continue with ASCII alphanumeric characters, _, or $
28572862
for c in chars {
2858-
// Inside a template format arg, any character is permitted for the puproses of label detection
2859-
// because we assume that it can be replaced with some other valid label string later.
2860-
// `options(raw)` asm blocks cannot have format args, so they are excluded from this special case.
2863+
// Inside a template format arg, any character is permitted for the
2864+
// puproses of label detection because we assume that it can be
2865+
// replaced with some other valid label string later. `options(raw)`
2866+
// asm blocks cannot have format args, so they are excluded from this
2867+
// special case.
28612868
if !raw && in_bracket {
28622869
if c == '{' {
2863-
// Nested brackets are not allowed in format args, this cannot be a label.
2870+
// Nested brackets are not allowed in format args, this cannot
2871+
// be a label.
28642872
break 'label_loop;
28652873
}
28662874

@@ -2873,7 +2881,8 @@ impl<'tcx> LateLintPass<'tcx> for NamedAsmLabels {
28732881
in_bracket = true;
28742882
} else {
28752883
if !(c.is_ascii_alphanumeric() || matches!(c, '_' | '$')) {
2876-
// The potential label had an invalid character inside it, it cannot be a label.
2884+
// The potential label had an invalid character inside it, it
2885+
// cannot be a label.
28772886
break 'label_loop;
28782887
}
28792888
}
@@ -2892,7 +2901,8 @@ impl<'tcx> LateLintPass<'tcx> for NamedAsmLabels {
28922901
.into_iter()
28932902
.filter_map(|label| find_label_span(label))
28942903
.collect::<Vec<Span>>();
2895-
// If there were labels but we couldn't find a span, combine the warnings and use the template span
2904+
// If there were labels but we couldn't find a span, combine the warnings and
2905+
// use the template span.
28962906
let target_spans: MultiSpan =
28972907
if spans.len() > 0 { spans.into() } else { (*template_span).into() };
28982908

compiler/rustc_lint/src/context.rs

+23-19
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ enum TargetLint {
9494

9595
/// A lint name that should give no warnings and have no effect.
9696
///
97-
/// This is used by rustc to avoid warning about old rustdoc lints before rustdoc registers them as tool lints.
97+
/// This is used by rustc to avoid warning about old rustdoc lints before rustdoc registers
98+
/// them as tool lints.
9899
Ignored,
99100
}
100101

@@ -126,12 +127,16 @@ pub enum CheckLintNameResult<'a> {
126127
Renamed(String),
127128
/// The lint has been removed due to the given reason.
128129
Removed(String),
129-
/// The lint is from a tool. If the Option is None, then either
130-
/// the lint does not exist in the tool or the code was not
131-
/// compiled with the tool and therefore the lint was never
132-
/// added to the `LintStore`. Otherwise the `LintId` will be
133-
/// returned as if it where a rustc lint.
134-
Tool(Result<&'a [LintId], (Option<&'a [LintId]>, String)>),
130+
131+
/// The lint is from a tool. The `LintId` will be returned as if it were a
132+
/// rustc lint. The `Option<String>` indicates if the lint has been
133+
/// renamed.
134+
Tool(&'a [LintId], Option<String>),
135+
136+
/// The lint is from a tool. Either the lint does not exist in the tool or
137+
/// the code was not compiled with the tool and therefore the lint was
138+
/// never added to the `LintStore`.
139+
MissingTool,
135140
}
136141

137142
impl LintStore {
@@ -384,14 +389,14 @@ impl LintStore {
384389
} else {
385390
// 2. The tool isn't currently running, so no lints will be registered.
386391
// To avoid giving a false positive, ignore all unknown lints.
387-
CheckLintNameResult::Tool(Err((None, String::new())))
392+
CheckLintNameResult::MissingTool
388393
};
389394
}
390395
Some(LintGroup { lint_ids, .. }) => {
391-
return CheckLintNameResult::Tool(Ok(lint_ids));
396+
return CheckLintNameResult::Tool(lint_ids, None);
392397
}
393398
},
394-
Some(Id(id)) => return CheckLintNameResult::Tool(Ok(slice::from_ref(id))),
399+
Some(Id(id)) => return CheckLintNameResult::Tool(slice::from_ref(id), None),
395400
// If the lint was registered as removed or renamed by the lint tool, we don't need
396401
// to treat tool_lints and rustc lints different and can use the code below.
397402
_ => {}
@@ -411,7 +416,7 @@ impl LintStore {
411416
return if *silent {
412417
CheckLintNameResult::Ok(lint_ids)
413418
} else {
414-
CheckLintNameResult::Tool(Err((Some(lint_ids), (*name).to_string())))
419+
CheckLintNameResult::Tool(lint_ids, Some((*name).to_string()))
415420
};
416421
}
417422
CheckLintNameResult::Ok(lint_ids)
@@ -472,18 +477,17 @@ impl LintStore {
472477
// Reaching this would be weird, but let's cover this case anyway
473478
if let Some(LintAlias { name, silent }) = depr {
474479
let LintGroup { lint_ids, .. } = self.lint_groups.get(name).unwrap();
475-
return if *silent {
476-
CheckLintNameResult::Tool(Err((Some(lint_ids), complete_name)))
480+
if *silent {
481+
CheckLintNameResult::Tool(lint_ids, Some(complete_name))
477482
} else {
478-
CheckLintNameResult::Tool(Err((Some(lint_ids), (*name).to_string())))
479-
};
483+
CheckLintNameResult::Tool(lint_ids, Some((*name).to_string()))
484+
}
485+
} else {
486+
CheckLintNameResult::Tool(lint_ids, Some(complete_name))
480487
}
481-
CheckLintNameResult::Tool(Err((Some(lint_ids), complete_name)))
482488
}
483489
},
484-
Some(Id(id)) => {
485-
CheckLintNameResult::Tool(Err((Some(slice::from_ref(id)), complete_name)))
486-
}
490+
Some(Id(id)) => CheckLintNameResult::Tool(slice::from_ref(id), Some(complete_name)),
487491
Some(other) => {
488492
debug!("got renamed lint {:?}", other);
489493
CheckLintNameResult::NoLint(None)

compiler/rustc_lint/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct OverruledAttribute<'a> {
1616
#[subdiagnostic]
1717
pub sub: OverruledAttributeSub,
1818
}
19-
//
19+
2020
pub enum OverruledAttributeSub {
2121
DefaultSource { id: String },
2222
NodeSource { span: Span, reason: Option<Symbol> },

compiler/rustc_lint/src/internal.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ use rustc_span::Span;
1818
use tracing::debug;
1919

2020
declare_tool_lint! {
21-
/// The `default_hash_type` lint detects use of [`std::collections::HashMap`]/[`std::collections::HashSet`],
22-
/// suggesting the use of `FxHashMap`/`FxHashSet`.
21+
/// The `default_hash_type` lint detects use of [`std::collections::HashMap`] and
22+
/// [`std::collections::HashSet`], suggesting the use of `FxHashMap`/`FxHashSet`.
2323
///
24-
/// This can help as `FxHasher` can perform better than the default hasher. DOS protection is not
25-
/// required as input is assumed to be trusted.
24+
/// This can help as `FxHasher` can perform better than the default hasher. DOS protection is
25+
/// not required as input is assumed to be trusted.
2626
pub rustc::DEFAULT_HASH_TYPES,
2727
Allow,
2828
"forbid HashMap and HashSet and suggest the FxHash* variants",
@@ -35,7 +35,7 @@ impl LateLintPass<'_> for DefaultHashTypes {
3535
fn check_path(&mut self, cx: &LateContext<'_>, path: &Path<'_>, hir_id: HirId) {
3636
let Res::Def(rustc_hir::def::DefKind::Struct, def_id) = path.res else { return };
3737
if matches!(cx.tcx.hir_node(hir_id), Node::Item(Item { kind: ItemKind::Use(..), .. })) {
38-
// don't lint imports, only actual usages
38+
// Don't lint imports, only actual usages.
3939
return;
4040
}
4141
let preferred = match cx.tcx.get_diagnostic_name(def_id) {
@@ -75,8 +75,8 @@ declare_tool_lint! {
7575
/// potential query instability, such as iterating over a `HashMap`.
7676
///
7777
/// Due to the [incremental compilation](https://rustc-dev-guide.rust-lang.org/queries/incremental-compilation.html) model,
78-
/// queries must return deterministic, stable results. `HashMap` iteration order can change between compilations,
79-
/// and will introduce instability if query results expose the order.
78+
/// queries must return deterministic, stable results. `HashMap` iteration order can change
79+
/// between compilations, and will introduce instability if query results expose the order.
8080
pub rustc::POTENTIAL_QUERY_INSTABILITY,
8181
Allow,
8282
"require explicit opt-in when using potentially unstable methods or functions",

compiler/rustc_lint/src/let_underscore.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
113113

114114
let mut top_level = true;
115115

116-
// We recursively walk through all patterns, so that we can catch cases where the lock is nested in a pattern.
117-
// For the basic `let_underscore_drop` lint, we only look at the top level, since there are many legitimate reasons
118-
// to bind a sub-pattern to an `_`, if we're only interested in the rest.
119-
// But with locks, we prefer having the chance of "false positives" over missing cases, since the effects can be
120-
// quite catastrophic.
116+
// We recursively walk through all patterns, so that we can catch cases where the lock is
117+
// nested in a pattern. For the basic `let_underscore_drop` lint, we only look at the top
118+
// level, since there are many legitimate reasons to bind a sub-pattern to an `_`, if we're
119+
// only interested in the rest. But with locks, we prefer having the chance of "false
120+
// positives" over missing cases, since the effects can be quite catastrophic.
121121
local.pat.walk_always(|pat| {
122122
let is_top_level = top_level;
123123
top_level = false;

0 commit comments

Comments
 (0)