File tree Expand file tree Collapse file tree
crates/emmylua_code_analysis/src/diagnostic Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -270,22 +270,32 @@ fn check_return_count(
270270 } ;
271271
272272 // 计算实际返回的表达式数量并记录多余的范围
273- let expr_list = return_stat. get_expr_list ( ) ;
273+ let expr_list = return_stat. get_expr_list ( ) . collect :: < Vec < _ > > ( ) ;
274274 let mut total_return_count = 0 ;
275+ let mut tail_return_nil = false ;
275276 let mut redundant_ranges = Vec :: new ( ) ;
276277
277- for expr in expr_list {
278+ for ( index , expr) in expr_list. iter ( ) . enumerate ( ) {
278279 let expr_type = semantic_model
279280 . infer_expr ( expr. clone ( ) )
280281 . unwrap_or ( LuaType :: Unknown ) ;
281282 match expr_type {
282283 LuaType :: Variadic ( variadic) => {
283284 total_return_count += variadic. get_max_len ( ) ?;
284285 }
286+ LuaType :: Nil => {
287+ if index == expr_list. len ( ) - 1 {
288+ tail_return_nil = true ;
289+ }
290+ total_return_count += 1 ;
291+ }
285292 _ => total_return_count += 1 ,
286293 } ;
287294
288295 if max_expected_return_count. is_some ( ) && total_return_count > max_expected_return_count? {
296+ if tail_return_nil && total_return_count - 1 == max_expected_return_count? {
297+ continue ;
298+ }
289299 redundant_ranges. push ( expr. get_range ( ) ) ;
290300 }
291301 }
Original file line number Diff line number Diff line change @@ -524,4 +524,31 @@ mod tests {
524524 "# ,
525525 ) ) ;
526526 }
527+
528+ #[ test]
529+ fn test_issue_567 ( ) {
530+ let mut ws = VirtualWorkspace :: new_with_init_std_lib ( ) ;
531+ assert ! ( ws. check_code_for(
532+ DiagnosticCode :: RedundantReturnValue ,
533+ r#"
534+ local function fnil()
535+ end
536+
537+ local f --- @type fun(c: fun())
538+ f(function()
539+ return fnil()
540+ end)
541+ "# ,
542+ ) ) ;
543+
544+ assert ! ( ws. check_code_for(
545+ DiagnosticCode :: RedundantReturnValue ,
546+ r#"
547+ --- @return nil
548+ local function f1()
549+ return nil
550+ end
551+ "# ,
552+ ) ) ;
553+ }
527554}
You can’t perform that action at this time.
0 commit comments