Skip to content

Commit 25cce39

Browse files
committed
[red-knot] Allow any Ranged argument for report_lint and report_diagnostic
1 parent e84985e commit 25cce39

File tree

6 files changed

+228
-215
lines changed

6 files changed

+228
-215
lines changed

crates/red_knot_python_semantic/src/types.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,11 +2264,7 @@ impl<'db> InvalidTypeExpressionError<'db> {
22642264
invalid_expressions,
22652265
} = self;
22662266
for error in invalid_expressions {
2267-
context.report_lint(
2268-
&INVALID_TYPE_FORM,
2269-
node.into(),
2270-
format_args!("{}", error.reason()),
2271-
);
2267+
context.report_lint(&INVALID_TYPE_FORM, node, format_args!("{}", error.reason()));
22722268
}
22732269
fallback_type
22742270
}

crates/red_knot_python_semantic/src/types/context.rs

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use ruff_db::{
55
diagnostic::{DiagnosticId, Severity},
66
files::File,
77
};
8-
use ruff_python_ast::AnyNodeRef;
9-
use ruff_text_size::Ranged;
8+
use ruff_text_size::{Ranged, TextRange};
109

1110
use super::{binding_type, KnownFunction, TypeCheckDiagnostic, TypeCheckDiagnostics};
1211

@@ -67,46 +66,60 @@ impl<'db> InferContext<'db> {
6766
self.diagnostics.get_mut().extend(other.diagnostics());
6867
}
6968

70-
/// Reports a lint located at `node`.
71-
pub(super) fn report_lint(
69+
/// Reports a lint located at `ranged`.
70+
pub(super) fn report_lint<T>(
7271
&self,
7372
lint: &'static LintMetadata,
74-
node: AnyNodeRef,
73+
ranged: T,
7574
message: fmt::Arguments,
76-
) {
77-
if !self.db.is_file_open(self.file) {
78-
return;
79-
}
75+
) where
76+
T: Ranged,
77+
{
78+
fn lint_severity(
79+
context: &InferContext,
80+
lint: &'static LintMetadata,
81+
range: TextRange,
82+
) -> Option<Severity> {
83+
if !context.db.is_file_open(context.file) {
84+
return None;
85+
}
8086

81-
// Skip over diagnostics if the rule is disabled.
82-
let Some(severity) = self.db.rule_selection().severity(LintId::of(lint)) else {
83-
return;
84-
};
87+
// Skip over diagnostics if the rule is disabled.
88+
let severity = context.db.rule_selection().severity(LintId::of(lint))?;
8589

86-
if self.is_in_no_type_check() {
87-
return;
88-
}
90+
if context.is_in_no_type_check() {
91+
return None;
92+
}
8993

90-
let suppressions = suppressions(self.db, self.file);
94+
let suppressions = suppressions(context.db, context.file);
9195

92-
if let Some(suppression) = suppressions.find_suppression(node.range(), LintId::of(lint)) {
93-
self.diagnostics.borrow_mut().mark_used(suppression.id());
94-
return;
96+
if let Some(suppression) = suppressions.find_suppression(range, LintId::of(lint)) {
97+
context.diagnostics.borrow_mut().mark_used(suppression.id());
98+
return None;
99+
}
100+
101+
Some(severity)
95102
}
96103

97-
self.report_diagnostic(node, DiagnosticId::Lint(lint.name()), severity, message);
104+
let Some(severity) = lint_severity(self, lint, ranged.range()) else {
105+
return;
106+
};
107+
108+
self.report_diagnostic(ranged, DiagnosticId::Lint(lint.name()), severity, message);
98109
}
99110

100111
/// Adds a new diagnostic.
101112
///
102113
/// The diagnostic does not get added if the rule isn't enabled for this file.
103-
pub(super) fn report_diagnostic(
114+
pub(super) fn report_diagnostic<T>(
104115
&self,
105-
node: AnyNodeRef,
116+
ranged: T,
106117
id: DiagnosticId,
107118
severity: Severity,
108119
message: fmt::Arguments,
109-
) {
120+
) where
121+
T: Ranged,
122+
{
110123
if !self.db.is_file_open(self.file) {
111124
return;
112125
}
@@ -121,7 +134,7 @@ impl<'db> InferContext<'db> {
121134
file: self.file,
122135
id,
123136
message: message.to_string(),
124-
range: node.range(),
137+
range: ranged.range(),
125138
severity,
126139
});
127140
}

crates/red_knot_python_semantic/src/types/diagnostic.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ pub(super) fn report_possibly_unresolved_reference(
10481048

10491049
context.report_lint(
10501050
&POSSIBLY_UNRESOLVED_REFERENCE,
1051-
expr_name_node.into(),
1051+
expr_name_node,
10521052
format_args!("Name `{id}` used when possibly not defined"),
10531053
);
10541054
}
@@ -1058,15 +1058,15 @@ pub(super) fn report_unresolved_reference(context: &InferContext, expr_name_node
10581058

10591059
context.report_lint(
10601060
&UNRESOLVED_REFERENCE,
1061-
expr_name_node.into(),
1061+
expr_name_node,
10621062
format_args!("Name `{id}` used when not defined"),
10631063
);
10641064
}
10651065

10661066
pub(super) fn report_invalid_exception_caught(context: &InferContext, node: &ast::Expr, ty: Type) {
10671067
context.report_lint(
10681068
&INVALID_EXCEPTION_CAUGHT,
1069-
node.into(),
1069+
node,
10701070
format_args!(
10711071
"Cannot catch object of type `{}` in an exception handler \
10721072
(must be a `BaseException` subclass or a tuple of `BaseException` subclasses)",
@@ -1078,7 +1078,7 @@ pub(super) fn report_invalid_exception_caught(context: &InferContext, node: &ast
10781078
pub(crate) fn report_invalid_exception_raised(context: &InferContext, node: &ast::Expr, ty: Type) {
10791079
context.report_lint(
10801080
&INVALID_RAISE,
1081-
node.into(),
1081+
node,
10821082
format_args!(
10831083
"Cannot raise object of type `{}` (must be a `BaseException` subclass or instance)",
10841084
ty.display(context.db())
@@ -1089,7 +1089,7 @@ pub(crate) fn report_invalid_exception_raised(context: &InferContext, node: &ast
10891089
pub(crate) fn report_invalid_exception_cause(context: &InferContext, node: &ast::Expr, ty: Type) {
10901090
context.report_lint(
10911091
&INVALID_RAISE,
1092-
node.into(),
1092+
node,
10931093
format_args!(
10941094
"Cannot use object of type `{}` as exception cause \
10951095
(must be a `BaseException` subclass or instance or `None`)",
@@ -1101,7 +1101,7 @@ pub(crate) fn report_invalid_exception_cause(context: &InferContext, node: &ast:
11011101
pub(crate) fn report_base_with_incompatible_slots(context: &InferContext, node: &ast::Expr) {
11021102
context.report_lint(
11031103
&INCOMPATIBLE_SLOTS,
1104-
node.into(),
1104+
node,
11051105
format_args!("Class base has incompatible `__slots__`"),
11061106
);
11071107
}
@@ -1113,7 +1113,7 @@ pub(crate) fn report_invalid_arguments_to_annotated<'db>(
11131113
) {
11141114
context.report_lint(
11151115
&INVALID_TYPE_FORM,
1116-
subscript.into(),
1116+
subscript,
11171117
format_args!(
11181118
"Special form `{}` expected at least 2 arguments (one type and at least one metadata element)",
11191119
KnownInstanceType::Annotated.repr(db)

0 commit comments

Comments
 (0)