Skip to content

Commit ff1fc68

Browse files
committed
Simplify lint source computation.
1 parent 5f1f45b commit ff1fc68

File tree

1 file changed

+83
-105
lines changed

1 file changed

+83
-105
lines changed

compiler/rustc_lint/src/levels.rs

+83-105
Original file line numberDiff line numberDiff line change
@@ -695,59 +695,20 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
695695
let name = pprust::path_to_string(&meta_item.path);
696696
let lint_result =
697697
self.store.check_lint_name(&name, tool_name, self.registered_tools);
698-
match &lint_result {
698+
699+
let (ids, name) = match lint_result {
699700
CheckLintNameResult::Ok(ids) => {
700-
// This checks for instances where the user writes
701-
// `#[expect(unfulfilled_lint_expectations)]` in that case we want to avoid
702-
// overriding the lint level but instead add an expectation that can't be
703-
// fulfilled. The lint message will include an explanation, that the
704-
// `unfulfilled_lint_expectations` lint can't be expected.
705-
if let Level::Expect(expect_id) = level {
706-
// The `unfulfilled_lint_expectations` lint is not part of any lint
707-
// groups. Therefore. we only need to check the slice if it contains a
708-
// single lint.
709-
let is_unfulfilled_lint_expectations = match ids {
710-
[lint] => *lint == LintId::of(UNFULFILLED_LINT_EXPECTATIONS),
711-
_ => false,
712-
};
713-
self.provider.push_expectation(
714-
expect_id,
715-
LintExpectation::new(
716-
reason,
717-
sp,
718-
is_unfulfilled_lint_expectations,
719-
tool_name,
720-
),
721-
);
722-
}
723-
let src = LintLevelSource::Node {
724-
name: meta_item
725-
.path
726-
.segments
727-
.last()
728-
.expect("empty lint name")
729-
.ident
730-
.name,
731-
span: sp,
732-
reason,
733-
};
734-
for &id in *ids {
735-
if self.check_gated_lint(id, attr.span, false) {
736-
self.insert_spec(id, (level, src));
737-
}
738-
}
701+
let name =
702+
meta_item.path.segments.last().expect("empty lint name").ident.name;
703+
(ids, name)
739704
}
740705

741706
CheckLintNameResult::Tool(ids, new_lint_name) => {
742-
let src = match new_lint_name {
707+
let name = match new_lint_name {
743708
None => {
744709
let complete_name =
745710
&format!("{}::{}", tool_ident.unwrap().name, name);
746-
LintLevelSource::Node {
747-
name: Symbol::intern(complete_name),
748-
span: sp,
749-
reason,
750-
}
711+
Symbol::intern(complete_name)
751712
}
752713
Some(new_lint_name) => {
753714
self.emit_span_lint(
@@ -756,37 +717,24 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
756717
DeprecatedLintName {
757718
name,
758719
suggestion: sp,
759-
replace: new_lint_name,
720+
replace: &new_lint_name,
760721
},
761722
);
762-
LintLevelSource::Node {
763-
name: Symbol::intern(new_lint_name),
764-
span: sp,
765-
reason,
766-
}
723+
Symbol::intern(&new_lint_name)
767724
}
768725
};
769-
for &id in *ids {
770-
if self.check_gated_lint(id, attr.span, false) {
771-
self.insert_spec(id, (level, src));
772-
}
773-
}
774-
if let Level::Expect(expect_id) = level {
775-
self.provider.push_expectation(
776-
expect_id,
777-
LintExpectation::new(reason, sp, false, tool_name),
778-
);
779-
}
726+
(ids, name)
780727
}
781728

782729
CheckLintNameResult::MissingTool => {
783730
// If `MissingTool` is returned, then either the lint does not
784731
// exist in the tool or the code was not compiled with the tool and
785732
// therefore the lint was never added to the `LintStore`. To detect
786733
// this is the responsibility of the lint tool.
734+
continue;
787735
}
788736

789-
&CheckLintNameResult::NoTool => {
737+
CheckLintNameResult::NoTool => {
790738
sess.dcx().emit_err(UnknownToolInScopedLint {
791739
span: tool_ident.map(|ident| ident.span),
792740
tool_name: tool_name.unwrap(),
@@ -796,58 +744,88 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
796744
continue;
797745
}
798746

799-
_ if !self.lint_added_lints => {}
800-
801747
CheckLintNameResult::Renamed(ref replace) => {
802-
let suggestion =
803-
RenamedLintSuggestion::WithSpan { suggestion: sp, replace };
804-
let name = tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
805-
let lint = RenamedLint { name: name.as_str(), suggestion };
806-
self.emit_span_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
748+
if self.lint_added_lints {
749+
let suggestion =
750+
RenamedLintSuggestion::WithSpan { suggestion: sp, replace };
751+
let name =
752+
tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
753+
let lint = RenamedLint { name: name.as_str(), suggestion };
754+
self.emit_span_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
755+
}
756+
757+
// If this lint was renamed, apply the new lint instead of ignoring the
758+
// attribute. Ignore any errors or warnings that happen because the new
759+
// name is inaccurate.
760+
// NOTE: `new_name` already includes the tool name, so we don't
761+
// have to add it again.
762+
let CheckLintNameResult::Ok(ids) =
763+
self.store.check_lint_name(replace, None, self.registered_tools)
764+
else {
765+
panic!("renamed lint does not exist: {replace}");
766+
};
767+
768+
(ids, Symbol::intern(&replace))
807769
}
808770

809771
CheckLintNameResult::Removed(ref reason) => {
810-
let name = tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
811-
let lint = RemovedLint { name: name.as_str(), reason };
812-
self.emit_span_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
772+
if self.lint_added_lints {
773+
let name =
774+
tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
775+
let lint = RemovedLint { name: name.as_str(), reason };
776+
self.emit_span_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
777+
}
778+
continue;
813779
}
814780

815781
CheckLintNameResult::NoLint(suggestion) => {
816-
let name = tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
817-
let suggestion = suggestion.map(|(replace, from_rustc)| {
818-
UnknownLintSuggestion::WithSpan { suggestion: sp, replace, from_rustc }
819-
});
820-
let lint = UnknownLint { name, suggestion };
821-
self.emit_span_lint(UNKNOWN_LINTS, sp.into(), lint);
822-
}
823-
}
824-
// If this lint was renamed, apply the new lint instead of ignoring the attribute.
825-
// This happens outside of the match because the new lint should be applied even if
826-
// we don't warn about the name change.
827-
if let CheckLintNameResult::Renamed(new_name) = lint_result {
828-
// Ignore any errors or warnings that happen because the new name is inaccurate
829-
// NOTE: `new_name` already includes the tool name, so we don't have to add it
830-
// again.
831-
let CheckLintNameResult::Ok(ids) =
832-
self.store.check_lint_name(&new_name, None, self.registered_tools)
833-
else {
834-
panic!("renamed lint does not exist: {new_name}");
835-
};
836-
837-
let src =
838-
LintLevelSource::Node { name: Symbol::intern(&new_name), span: sp, reason };
839-
for &id in ids {
840-
if self.check_gated_lint(id, attr.span, false) {
841-
self.insert_spec(id, (level, src));
782+
if self.lint_added_lints {
783+
let name =
784+
tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
785+
let suggestion = suggestion.map(|(replace, from_rustc)| {
786+
UnknownLintSuggestion::WithSpan {
787+
suggestion: sp,
788+
replace,
789+
from_rustc,
790+
}
791+
});
792+
let lint = UnknownLint { name, suggestion };
793+
self.emit_span_lint(UNKNOWN_LINTS, sp.into(), lint);
842794
}
795+
continue;
843796
}
844-
if let Level::Expect(expect_id) = level {
845-
self.provider.push_expectation(
846-
expect_id,
847-
LintExpectation::new(reason, sp, false, tool_name),
848-
);
797+
};
798+
799+
let src = LintLevelSource::Node { name, span: sp, reason };
800+
for &id in ids {
801+
if self.check_gated_lint(id, attr.span, false) {
802+
self.insert_spec(id, (level, src));
849803
}
850804
}
805+
806+
// This checks for instances where the user writes
807+
// `#[expect(unfulfilled_lint_expectations)]` in that case we want to avoid
808+
// overriding the lint level but instead add an expectation that can't be
809+
// fulfilled. The lint message will include an explanation, that the
810+
// `unfulfilled_lint_expectations` lint can't be expected.
811+
if let Level::Expect(expect_id) = level {
812+
// The `unfulfilled_lint_expectations` lint is not part of any lint
813+
// groups. Therefore. we only need to check the slice if it contains a
814+
// single lint.
815+
let is_unfulfilled_lint_expectations = match ids {
816+
[lint] => *lint == LintId::of(UNFULFILLED_LINT_EXPECTATIONS),
817+
_ => false,
818+
};
819+
self.provider.push_expectation(
820+
expect_id,
821+
LintExpectation::new(
822+
reason,
823+
sp,
824+
is_unfulfilled_lint_expectations,
825+
tool_name,
826+
),
827+
);
828+
}
851829
}
852830
}
853831

0 commit comments

Comments
 (0)