Skip to content

Commit 6c24bff

Browse files
committed
fix #894
1 parent 14f72d7 commit 6c24bff

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

crates/emmylua_code_analysis/src/diagnostic/checker/check_param_count.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ fn check_call_expr(
9696
let mut fake_params = func.get_params().to_vec();
9797
let call_args = call_expr.get_args_list()?.get_args().collect::<Vec<_>>();
9898
let mut call_args_count = call_args.len();
99+
let last_arg_is_dots = call_args.last().is_some_and(is_dots_expr);
99100
// 根据冒号定义与冒号调用的情况来调整调用参数的数量
100101
let colon_call = call_expr.is_colon_call();
101102
let colon_define = func.is_colon_define();
@@ -171,7 +172,16 @@ fn check_call_expr(
171172
}
172173
}
173174
// Check for redundant parameters
174-
else if call_args_count > fake_params.len() {
175+
else {
176+
let mut min_call_args_count = call_args_count;
177+
if last_arg_is_dots {
178+
min_call_args_count = min_call_args_count.saturating_sub(1);
179+
}
180+
181+
if min_call_args_count <= fake_params.len() {
182+
return Some(());
183+
}
184+
175185
// 参数定义中最后一个参数是 `...`
176186
if fake_params.last().is_some_and(|(name, typ)| {
177187
name == "..." || typ.as_ref().is_some_and(|typ| typ.is_variadic())
@@ -185,6 +195,10 @@ fn check_call_expr(
185195
}
186196

187197
for (i, arg) in call_args.iter().enumerate() {
198+
if last_arg_is_dots && i + 1 == call_args.len() {
199+
continue;
200+
}
201+
188202
let param_index = i as isize + adjusted_index;
189203

190204
if param_index < 0 || param_index < fake_params.len() as isize {
@@ -197,7 +211,7 @@ fn check_call_expr(
197211
t!(
198212
"expected %{num} parameters but found %{found_num}",
199213
num = fake_params.len(),
200-
found_num = call_args_count,
214+
found_num = min_call_args_count,
201215
)
202216
.to_string(),
203217
None,
@@ -208,6 +222,15 @@ fn check_call_expr(
208222
Some(())
209223
}
210224

225+
fn is_dots_expr(expr: &LuaExpr) -> bool {
226+
if let LuaExpr::LiteralExpr(literal_expr) = expr
227+
&& let Some(LuaLiteralToken::Dots(_)) = literal_expr.get_literal()
228+
{
229+
return true;
230+
}
231+
false
232+
}
233+
211234
fn get_params_len(params: &[(String, Option<LuaType>)]) -> Option<usize> {
212235
if let Some((name, typ)) = params.last() {
213236
// 如果最后一个参数是可变参数, 则直接返回, 不需要检查

crates/emmylua_code_analysis/src/diagnostic/test/redundant_parameter_test.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,20 @@ mod test {
159159
"#
160160
));
161161
}
162+
163+
#[test]
164+
fn test_issue_894() {
165+
let mut ws = VirtualWorkspace::new();
166+
ws.def(
167+
r#"
168+
_nop = function() end
169+
"#,
170+
);
171+
assert!(ws.check_code_for(
172+
DiagnosticCode::RedundantParameter,
173+
r#"
174+
function a(...) _nop(...) end
175+
"#
176+
));
177+
}
162178
}

0 commit comments

Comments
 (0)