@@ -150,29 +150,11 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
150
150
expected_case : CaseType :: LowerSnakeCase ,
151
151
} ) ;
152
152
153
- // Check the param names.
154
- let fn_param_replacements = body
155
- . params
156
- . iter ( )
157
- . filter_map ( |& id| match & body[ id] {
158
- Pat :: Bind { name, .. } => Some ( name) ,
159
- _ => None ,
160
- } )
161
- . filter_map ( |param_name| {
162
- Some ( Replacement {
163
- current_name : param_name. clone ( ) ,
164
- suggested_text : to_lower_snake_case ( & param_name. to_string ( ) ) ?,
165
- expected_case : CaseType :: LowerSnakeCase ,
166
- } )
167
- } )
168
- . collect ( ) ;
169
-
170
153
// Check the patterns inside the function body.
154
+ // This includes function parameters.
171
155
let pats_replacements = body
172
156
. pats
173
157
. iter ( )
174
- // We aren't interested in function parameters, we've processed them above.
175
- . filter ( |( pat_idx, _) | !body. params . contains ( & pat_idx) )
176
158
. filter_map ( |( id, pat) | match pat {
177
159
Pat :: Bind { name, .. } => Some ( ( id, name) ) ,
178
160
_ => None ,
@@ -190,11 +172,10 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
190
172
. collect ( ) ;
191
173
192
174
// If there is at least one element to spawn a warning on, go to the source map and generate a warning.
193
- self . create_incorrect_case_diagnostic_for_func (
194
- func,
195
- fn_name_replacement,
196
- fn_param_replacements,
197
- ) ;
175
+ if let Some ( fn_name_replacement) = fn_name_replacement {
176
+ self . create_incorrect_case_diagnostic_for_func ( func, fn_name_replacement) ;
177
+ }
178
+
198
179
self . create_incorrect_case_diagnostic_for_variables ( func, pats_replacements) ;
199
180
}
200
181
@@ -203,100 +184,34 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
203
184
fn create_incorrect_case_diagnostic_for_func (
204
185
& mut self ,
205
186
func : FunctionId ,
206
- fn_name_replacement : Option < Replacement > ,
207
- fn_param_replacements : Vec < Replacement > ,
187
+ fn_name_replacement : Replacement ,
208
188
) {
209
- // XXX: only look at sources if we do have incorrect names
210
- if fn_name_replacement. is_none ( ) && fn_param_replacements. is_empty ( ) {
211
- return ;
212
- }
213
-
214
189
let fn_loc = func. lookup ( self . db . upcast ( ) ) ;
215
190
let fn_src = fn_loc. source ( self . db . upcast ( ) ) ;
216
191
217
192
// Diagnostic for function name.
218
- if let Some ( replacement) = fn_name_replacement {
219
- let ast_ptr = match fn_src. value . name ( ) {
220
- Some ( name) => name,
221
- None => {
222
- never ! (
223
- "Replacement ({:?}) was generated for a function without a name: {:?}" ,
224
- replacement,
225
- fn_src
226
- ) ;
227
- return ;
228
- }
229
- } ;
230
-
231
- let diagnostic = IncorrectCase {
232
- file : fn_src. file_id ,
233
- ident_type : IdentType :: Function ,
234
- ident : AstPtr :: new ( & ast_ptr) ,
235
- expected_case : replacement. expected_case ,
236
- ident_text : replacement. current_name . to_string ( ) ,
237
- suggested_text : replacement. suggested_text ,
238
- } ;
239
-
240
- self . sink . push ( diagnostic) ;
241
- }
242
-
243
- // Diagnostics for function params.
244
- let fn_params_list = match fn_src. value . param_list ( ) {
245
- Some ( params) => params,
193
+ let ast_ptr = match fn_src. value . name ( ) {
194
+ Some ( name) => name,
246
195
None => {
247
- always ! (
248
- fn_param_replacements. is_empty( ) ,
249
- "Replacements ({:?}) were generated for a function parameters which had no parameters list: {:?}" ,
250
- fn_param_replacements,
196
+ never ! (
197
+ "Replacement ({:?}) was generated for a function without a name: {:?}" ,
198
+ fn_name_replacement,
251
199
fn_src
252
200
) ;
253
201
return ;
254
202
}
255
203
} ;
256
- let mut fn_params_iter = fn_params_list. params ( ) ;
257
- for param_to_rename in fn_param_replacements {
258
- // We assume that parameters in replacement are in the same order as in the
259
- // actual params list, but just some of them (ones that named correctly) are skipped.
260
- let ast_ptr: ast:: Name = loop {
261
- match fn_params_iter. next ( ) {
262
- Some ( element) => {
263
- if let Some ( ast:: Pat :: IdentPat ( pat) ) = element. pat ( ) {
264
- if pat. to_string ( ) == param_to_rename. current_name . to_string ( ) {
265
- if let Some ( name) = pat. name ( ) {
266
- break name;
267
- }
268
- // This is critical. If we consider this parameter the expected one,
269
- // it **must** have a name.
270
- never ! (
271
- "Pattern {:?} equals to expected replacement {:?}, but has no name" ,
272
- element,
273
- param_to_rename
274
- ) ;
275
- return ;
276
- }
277
- }
278
- }
279
- None => {
280
- never ! (
281
- "Replacement ({:?}) was generated for a function parameter which was not found: {:?}" ,
282
- param_to_rename, fn_src
283
- ) ;
284
- return ;
285
- }
286
- }
287
- } ;
288
204
289
- let diagnostic = IncorrectCase {
290
- file : fn_src. file_id ,
291
- ident_type : IdentType :: Argument ,
292
- ident : AstPtr :: new ( & ast_ptr) ,
293
- expected_case : param_to_rename . expected_case ,
294
- ident_text : param_to_rename . current_name . to_string ( ) ,
295
- suggested_text : param_to_rename . suggested_text ,
296
- } ;
205
+ let diagnostic = IncorrectCase {
206
+ file : fn_src. file_id ,
207
+ ident_type : IdentType :: Function ,
208
+ ident : AstPtr :: new ( & ast_ptr) ,
209
+ expected_case : fn_name_replacement . expected_case ,
210
+ ident_text : fn_name_replacement . current_name . to_string ( ) ,
211
+ suggested_text : fn_name_replacement . suggested_text ,
212
+ } ;
297
213
298
- self . sink . push ( diagnostic) ;
299
- }
214
+ self . sink . push ( diagnostic) ;
300
215
}
301
216
302
217
/// Given the information about incorrect variable names, looks up into the source code
@@ -327,20 +242,25 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
327
242
None => continue ,
328
243
} ;
329
244
245
+ let is_param = ast:: Param :: can_cast ( parent. kind ( ) ) ;
246
+
330
247
// We have to check that it's either `let var = ...` or `var @ Variant(_)` statement,
331
248
// because e.g. match arms are patterns as well.
332
249
// In other words, we check that it's a named variable binding.
333
250
let is_binding = ast:: LetStmt :: can_cast ( parent. kind ( ) )
334
251
|| ( ast:: MatchArm :: can_cast ( parent. kind ( ) )
335
252
&& ident_pat. at_token ( ) . is_some ( ) ) ;
336
- if !is_binding {
253
+ if !( is_param || is_binding) {
337
254
// This pattern is not an actual variable declaration, e.g. `Some(val) => {..}` match arm.
338
255
continue ;
339
256
}
340
257
258
+ let ident_type =
259
+ if is_param { IdentType :: Parameter } else { IdentType :: Variable } ;
260
+
341
261
let diagnostic = IncorrectCase {
342
262
file : source_ptr. file_id ,
343
- ident_type : IdentType :: Variable ,
263
+ ident_type,
344
264
ident : AstPtr :: new ( & name_ast) ,
345
265
expected_case : replacement. expected_case ,
346
266
ident_text : replacement. current_name . to_string ( ) ,
@@ -408,7 +328,7 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
408
328
struct_name_replacement : Option < Replacement > ,
409
329
struct_fields_replacements : Vec < Replacement > ,
410
330
) {
411
- // XXX: only look at sources if we do have incorrect names
331
+ // XXX: Only look at sources if we do have incorrect names.
412
332
if struct_name_replacement. is_none ( ) && struct_fields_replacements. is_empty ( ) {
413
333
return ;
414
334
}
@@ -723,10 +643,10 @@ fn NonSnakeCaseName() {}
723
643
check_diagnostics (
724
644
r#"
725
645
fn foo(SomeParam: u8) {}
726
- // ^^^^^^^^^ Argument `SomeParam` should have snake_case name, e.g. `some_param`
646
+ // ^^^^^^^^^ Parameter `SomeParam` should have snake_case name, e.g. `some_param`
727
647
728
648
fn foo2(ok_param: &str, CAPS_PARAM: u8) {}
729
- // ^^^^^^^^^^ Argument `CAPS_PARAM` should have snake_case name, e.g. `caps_param`
649
+ // ^^^^^^^^^^ Parameter `CAPS_PARAM` should have snake_case name, e.g. `caps_param`
730
650
"# ,
731
651
) ;
732
652
}
@@ -1037,4 +957,9 @@ fn qualify() {
1037
957
"# ,
1038
958
)
1039
959
}
960
+
961
+ #[ test] // Issue #8809.
962
+ fn parenthesized_parameter ( ) {
963
+ check_diagnostics ( r#"fn f((O): _) {}"# )
964
+ }
1040
965
}
0 commit comments