Skip to content

Commit 66a9985

Browse files
committed
Simplify lint source computation.
1 parent 59c132e commit 66a9985

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
@@ -704,59 +704,20 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
704704
let name = pprust::path_to_string(&meta_item.path);
705705
let lint_result =
706706
self.store.check_lint_name(&name, tool_name, self.registered_tools);
707-
match &lint_result {
707+
708+
let (ids, name) = match lint_result {
708709
CheckLintNameResult::Ok(ids) => {
709-
// This checks for instances where the user writes
710-
// `#[expect(unfulfilled_lint_expectations)]` in that case we want to avoid
711-
// overriding the lint level but instead add an expectation that can't be
712-
// fulfilled. The lint message will include an explanation, that the
713-
// `unfulfilled_lint_expectations` lint can't be expected.
714-
if let Level::Expect(expect_id) = level {
715-
// The `unfulfilled_lint_expectations` lint is not part of any lint
716-
// groups. Therefore. we only need to check the slice if it contains a
717-
// single lint.
718-
let is_unfulfilled_lint_expectations = match ids {
719-
[lint] => *lint == LintId::of(UNFULFILLED_LINT_EXPECTATIONS),
720-
_ => false,
721-
};
722-
self.provider.push_expectation(
723-
expect_id,
724-
LintExpectation::new(
725-
reason,
726-
sp,
727-
is_unfulfilled_lint_expectations,
728-
tool_name,
729-
),
730-
);
731-
}
732-
let src = LintLevelSource::Node {
733-
name: meta_item
734-
.path
735-
.segments
736-
.last()
737-
.expect("empty lint name")
738-
.ident
739-
.name,
740-
span: sp,
741-
reason,
742-
};
743-
for &id in *ids {
744-
if self.check_gated_lint(id, attr.span, false) {
745-
self.insert_spec(id, (level, src));
746-
}
747-
}
710+
let name =
711+
meta_item.path.segments.last().expect("empty lint name").ident.name;
712+
(ids, name)
748713
}
749714

750715
CheckLintNameResult::Tool(ids, new_lint_name) => {
751-
let src = match new_lint_name {
716+
let name = match new_lint_name {
752717
None => {
753718
let complete_name =
754719
&format!("{}::{}", tool_ident.unwrap().name, name);
755-
LintLevelSource::Node {
756-
name: Symbol::intern(complete_name),
757-
span: sp,
758-
reason,
759-
}
720+
Symbol::intern(complete_name)
760721
}
761722
Some(new_lint_name) => {
762723
self.emit_span_lint(
@@ -765,37 +726,24 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
765726
DeprecatedLintName {
766727
name,
767728
suggestion: sp,
768-
replace: new_lint_name,
729+
replace: &new_lint_name,
769730
},
770731
);
771-
LintLevelSource::Node {
772-
name: Symbol::intern(new_lint_name),
773-
span: sp,
774-
reason,
775-
}
732+
Symbol::intern(&new_lint_name)
776733
}
777734
};
778-
for &id in *ids {
779-
if self.check_gated_lint(id, attr.span, false) {
780-
self.insert_spec(id, (level, src));
781-
}
782-
}
783-
if let Level::Expect(expect_id) = level {
784-
self.provider.push_expectation(
785-
expect_id,
786-
LintExpectation::new(reason, sp, false, tool_name),
787-
);
788-
}
735+
(ids, name)
789736
}
790737

791738
CheckLintNameResult::MissingTool => {
792739
// If `MissingTool` is returned, then either the lint does not
793740
// exist in the tool or the code was not compiled with the tool and
794741
// therefore the lint was never added to the `LintStore`. To detect
795742
// this is the responsibility of the lint tool.
743+
continue;
796744
}
797745

798-
&CheckLintNameResult::NoTool => {
746+
CheckLintNameResult::NoTool => {
799747
sess.dcx().emit_err(UnknownToolInScopedLint {
800748
span: tool_ident.map(|ident| ident.span),
801749
tool_name: tool_name.unwrap(),
@@ -805,58 +753,88 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
805753
continue;
806754
}
807755

808-
_ if !self.lint_added_lints => {}
809-
810756
CheckLintNameResult::Renamed(ref replace) => {
811-
let suggestion =
812-
RenamedLintSuggestion::WithSpan { suggestion: sp, replace };
813-
let name = tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
814-
let lint = RenamedLint { name: name.as_str(), suggestion };
815-
self.emit_span_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
757+
if self.lint_added_lints {
758+
let suggestion =
759+
RenamedLintSuggestion::WithSpan { suggestion: sp, replace };
760+
let name =
761+
tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
762+
let lint = RenamedLint { name: name.as_str(), suggestion };
763+
self.emit_span_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
764+
}
765+
766+
// If this lint was renamed, apply the new lint instead of ignoring the
767+
// attribute. Ignore any errors or warnings that happen because the new
768+
// name is inaccurate.
769+
// NOTE: `new_name` already includes the tool name, so we don't
770+
// have to add it again.
771+
let CheckLintNameResult::Ok(ids) =
772+
self.store.check_lint_name(replace, None, self.registered_tools)
773+
else {
774+
panic!("renamed lint does not exist: {replace}");
775+
};
776+
777+
(ids, Symbol::intern(&replace))
816778
}
817779

818780
CheckLintNameResult::Removed(ref reason) => {
819-
let name = tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
820-
let lint = RemovedLint { name: name.as_str(), reason };
821-
self.emit_span_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
781+
if self.lint_added_lints {
782+
let name =
783+
tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
784+
let lint = RemovedLint { name: name.as_str(), reason };
785+
self.emit_span_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
786+
}
787+
continue;
822788
}
823789

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

0 commit comments

Comments
 (0)