@@ -5,8 +5,7 @@ use ruff_db::{
55 diagnostic:: { DiagnosticId , SecondaryDiagnosticMessage , 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
1110use 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- self . report_lint_with_secondary_messages ( lint, node, message, vec ! [ ] ) ;
75+ ) where
76+ T : Ranged ,
77+ {
78+ self . report_lint_with_secondary_messages ( lint, ranged, message, vec ! [ ] ) ;
7879 }
7980
80- /// Reports a lint located at `node `.
81- pub ( super ) fn report_lint_with_secondary_messages (
81+ /// Reports a lint located at `ranged `.
82+ pub ( super ) fn report_lint_with_secondary_messages < T > (
8283 & self ,
8384 lint : & ' static LintMetadata ,
84- node : AnyNodeRef ,
85+ ranged : T ,
8586 message : fmt:: Arguments ,
8687 secondary_messages : Vec < SecondaryDiagnosticMessage > ,
87- ) {
88- if !self . db . is_file_open ( self . file ) {
89- return ;
90- }
88+ ) where
89+ T : Ranged ,
90+ {
91+ fn lint_severity (
92+ context : & InferContext ,
93+ lint : & ' static LintMetadata ,
94+ range : TextRange ,
95+ ) -> Option < Severity > {
96+ if !context. db . is_file_open ( context. file ) {
97+ return None ;
98+ }
9199
92- // Skip over diagnostics if the rule is disabled.
93- let Some ( severity) = self . db . rule_selection ( ) . severity ( LintId :: of ( lint) ) else {
94- return ;
95- } ;
100+ // Skip over diagnostics if the rule is disabled.
101+ let severity = context. db . rule_selection ( ) . severity ( LintId :: of ( lint) ) ?;
96102
97- if self . is_in_no_type_check ( ) {
98- return ;
99- }
103+ if context . is_in_no_type_check ( ) {
104+ return None ;
105+ }
100106
101- let suppressions = suppressions ( self . db , self . file ) ;
107+ let suppressions = suppressions ( context . db , context . file ) ;
102108
103- if let Some ( suppression) = suppressions. find_suppression ( node. range ( ) , LintId :: of ( lint) ) {
104- self . diagnostics . borrow_mut ( ) . mark_used ( suppression. id ( ) ) ;
105- return ;
109+ if let Some ( suppression) = suppressions. find_suppression ( range, LintId :: of ( lint) ) {
110+ context. diagnostics . borrow_mut ( ) . mark_used ( suppression. id ( ) ) ;
111+ return None ;
112+ }
113+
114+ Some ( severity)
106115 }
107116
117+ let Some ( severity) = lint_severity ( self , lint, ranged. range ( ) ) else {
118+ return ;
119+ } ;
120+
108121 self . report_diagnostic (
109- node ,
122+ ranged ,
110123 DiagnosticId :: Lint ( lint. name ( ) ) ,
111124 severity,
112125 message,
@@ -117,14 +130,16 @@ impl<'db> InferContext<'db> {
117130 /// Adds a new diagnostic.
118131 ///
119132 /// The diagnostic does not get added if the rule isn't enabled for this file.
120- pub ( super ) fn report_diagnostic (
133+ pub ( super ) fn report_diagnostic < T > (
121134 & self ,
122- node : AnyNodeRef ,
135+ ranged : T ,
123136 id : DiagnosticId ,
124137 severity : Severity ,
125138 message : fmt:: Arguments ,
126139 secondary_messages : Vec < SecondaryDiagnosticMessage > ,
127- ) {
140+ ) where
141+ T : Ranged ,
142+ {
128143 if !self . db . is_file_open ( self . file ) {
129144 return ;
130145 }
@@ -139,7 +154,7 @@ impl<'db> InferContext<'db> {
139154 file : self . file ,
140155 id,
141156 message : message. to_string ( ) ,
142- range : node . range ( ) ,
157+ range : ranged . range ( ) ,
143158 severity,
144159 secondary_messages,
145160 } ) ;
0 commit comments