11use crate :: {
2- ast_util:: { range , scopes:: AssignedValue } ,
2+ ast_util:: scopes:: AssignedValue ,
33 standard_library:: { Field , FieldKind , Observes } ,
44} ;
55
66use super :: * ;
77
8- use full_moon:: {
9- ast:: Ast ,
10- node:: Node ,
11- tokenizer:: { Symbol , TokenType } ,
12- } ;
8+ use full_moon:: ast:: Ast ;
139use regex:: Regex ;
1410use serde:: Deserialize ;
1511
@@ -55,7 +51,7 @@ impl Lint for UnusedVariableLint {
5551 } )
5652 }
5753
58- fn pass ( & self , ast : & Ast , context : & Context , ast_context : & AstContext ) -> Vec < Diagnostic > {
54+ fn pass ( & self , _ : & Ast , context : & Context , ast_context : & AstContext ) -> Vec < Diagnostic > {
5955 let mut diagnostics = Vec :: new ( ) ;
6056
6157 for ( _, variable) in ast_context
@@ -161,8 +157,7 @@ impl Lint for UnusedVariableLint {
161157 {
162158 let mut notes = Vec :: new ( ) ;
163159
164- let mut variable_range = variable. identifiers [ 0 ] ;
165- let mut fixed_code = format ! ( "_{}" , variable. name) ;
160+ let mut fixed_code = Some ( format ! ( "_{}" , variable. name) ) ;
166161
167162 if variable. is_self {
168163 if self . allow_unused_self {
@@ -173,24 +168,8 @@ impl Lint for UnusedVariableLint {
173168 notes
174169 . push ( "if you don't need it, consider using `.` instead of `:`" . to_owned ( ) ) ;
175170
176- // This is a very hacky and fragile way to get the colon in the function call. Can we do better?
177- // We can't just use variable start - 1 due to cases like `function a: b() end`
178- let mut last_colon_start = 0 ;
179- for token in ast. tokens ( ) {
180- let start_position: usize = range ( token) . 0 ;
181- if start_position >= variable_range. 0 {
182- break ;
183- }
184-
185- if let TokenType :: Symbol { symbol } = token. token ( ) . token_type ( ) {
186- if * symbol == Symbol :: Colon {
187- last_colon_start = range ( token) . 0 ;
188- }
189- }
190- }
191-
192- variable_range = ( last_colon_start, last_colon_start + 1 ) ;
193- fixed_code = "." . to_string ( ) ;
171+ // Automatically changing `:` to `.` would break any existing methods calls
172+ fixed_code = None ;
194173 }
195174
196175 let write_only = !analyzed_references. is_empty ( ) ;
@@ -202,7 +181,7 @@ impl Lint for UnusedVariableLint {
202181 } else {
203182 format ! ( "{} is defined, but never used" , variable. name)
204183 } ,
205- Label :: new ( variable_range ) ,
184+ Label :: new ( variable . identifiers [ 0 ] ) ,
206185 notes,
207186 analyzed_references
208187 . into_iter ( )
@@ -214,7 +193,7 @@ impl Lint for UnusedVariableLint {
214193 }
215194 } )
216195 . collect ( ) ,
217- Some ( fixed_code) ,
196+ fixed_code,
218197 ) ) ;
219198 } ;
220199 }
0 commit comments