Skip to content

Commit c8e65f9

Browse files
authored
Merge pull request #764 from ribru17/clippy_checkers
refactor: address the clippy warnings in checkers
2 parents cb88050 + d7876e6 commit c8e65f9

27 files changed

+368
-468
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ fn check_name_expr(
4040
)?;
4141

4242
let decl_id = LuaDeclId::new(semantic_model.get_file_id(), name_expr.get_position());
43-
if let LuaSemanticDeclId::LuaDecl(id) = &semantic_decl {
44-
if *id == decl_id {
45-
return Some(());
46-
}
43+
if let LuaSemanticDeclId::LuaDecl(id) = &semantic_decl
44+
&& *id == decl_id
45+
{
46+
return Some(());
4747
}
4848

4949
let name_token = name_expr.get_name_token()?;
5050
if !semantic_model.is_semantic_visible(name_token.syntax().clone(), semantic_decl.clone()) {
5151
let emmyrc = semantic_model.get_emmyrc();
52-
report_reason(context, &emmyrc, name_token.get_range(), semantic_decl);
52+
report_reason(context, emmyrc, name_token.get_range(), semantic_decl);
5353
}
5454
Some(())
5555
}
@@ -64,16 +64,16 @@ fn check_index_expr(
6464
SemanticDeclLevel::default(),
6565
)?;
6666
let member_id = LuaMemberId::new(index_expr.get_syntax_id(), semantic_model.get_file_id());
67-
if let LuaSemanticDeclId::Member(id) = &semantic_decl {
68-
if *id == member_id {
69-
return Some(());
70-
}
67+
if let LuaSemanticDeclId::Member(id) = &semantic_decl
68+
&& *id == member_id
69+
{
70+
return Some(());
7171
}
7272

7373
let index_token = index_expr.get_index_name_token()?;
7474
if !semantic_model.is_semantic_visible(index_token.clone(), semantic_decl.clone()) {
7575
let emmyrc = semantic_model.get_emmyrc();
76-
report_reason(context, &emmyrc, index_token.text_range(), semantic_decl);
76+
report_reason(context, emmyrc, index_token.text_range(), semantic_decl);
7777
}
7878

7979
Some(())

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

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn check_assign_stat(
4949
context,
5050
semantic_model,
5151
index_expr,
52-
exprs.get(idx).map(|expr| expr.clone()),
52+
exprs.get(idx).cloned(),
5353
value_types.get(idx)?.0.clone(),
5454
);
5555
}
@@ -58,7 +58,7 @@ fn check_assign_stat(
5858
context,
5959
semantic_model,
6060
name_expr,
61-
exprs.get(idx).map(|expr| expr.clone()),
61+
exprs.get(idx).cloned(),
6262
value_types.get(idx)?.0.clone(),
6363
);
6464
}
@@ -191,11 +191,11 @@ fn check_local_stat(
191191
&value_type,
192192
false,
193193
);
194-
if let Some(expr) = value_exprs.get(idx).map(|expr| expr) {
194+
if let Some(expr) = value_exprs.get(idx) {
195195
check_table_expr(
196196
context,
197197
semantic_model,
198-
&expr,
198+
expr,
199199
Some(&var_type),
200200
Some(&value_type),
201201
);
@@ -253,23 +253,21 @@ fn check_table_expr_content(
253253
.unwrap_or(LuaType::Any);
254254

255255
// 位于的最后的 TableFieldValue 允许接受函数调用返回的多值, 而且返回的值必然会从下标 1 开始覆盖掉所有索引字段.
256-
if field.is_value_field() && idx == fields.len() - 1 {
257-
match &expr_type {
258-
LuaType::Variadic(variadic) => {
259-
if let Some(result) = check_table_last_variadic_type(
260-
context,
261-
semantic_model,
262-
table_type,
263-
idx,
264-
&variadic,
265-
field.get_range(),
266-
) {
267-
has_diagnostic = has_diagnostic || result;
268-
}
269-
continue;
270-
}
271-
_ => {}
256+
if field.is_value_field()
257+
&& idx == fields.len() - 1
258+
&& let LuaType::Variadic(variadic) = &expr_type
259+
{
260+
if let Some(result) = check_table_last_variadic_type(
261+
context,
262+
semantic_model,
263+
table_type,
264+
idx,
265+
variadic,
266+
field.get_range(),
267+
) {
268+
has_diagnostic = has_diagnostic || result;
272269
}
270+
continue;
273271
}
274272

275273
let Some(field_key) = field.get_field_key() else {
@@ -278,29 +276,26 @@ fn check_table_expr_content(
278276
let Some(member_key) = semantic_model.get_member_key(&field_key) else {
279277
continue;
280278
};
281-
let source_type = match semantic_model.infer_member_type(&table_type, &member_key) {
279+
let source_type = match semantic_model.infer_member_type(table_type, &member_key) {
282280
Ok(typ) => typ,
283281
Err(_) => {
284282
continue;
285283
}
286284
};
287285

288-
if source_type.is_table() || source_type.is_custom_type() {
289-
if let Some(table_expr) = LuaTableExpr::cast(value_expr.syntax().clone()) {
290-
// 检查子表
291-
if let Some(result) =
292-
check_table_expr_content(context, semantic_model, &source_type, &table_expr)
293-
{
294-
has_diagnostic = has_diagnostic || result;
295-
}
296-
continue;
286+
if (source_type.is_table() || source_type.is_custom_type())
287+
&& let Some(table_expr) = LuaTableExpr::cast(value_expr.syntax().clone())
288+
{
289+
// 检查子表
290+
if let Some(result) =
291+
check_table_expr_content(context, semantic_model, &source_type, &table_expr)
292+
{
293+
has_diagnostic = has_diagnostic || result;
297294
}
295+
continue;
298296
}
299297

300-
let allow_nil = match table_type {
301-
LuaType::Array(_) => true,
302-
_ => false,
303-
};
298+
let allow_nil = matches!(table_type, LuaType::Array(_));
304299

305300
if let Some(result) = check_assign_type_mismatch(
306301
context,
@@ -329,7 +324,7 @@ fn check_table_last_variadic_type(
329324
for offset in idx..(idx + 10) {
330325
let member_key = LuaMemberKey::Integer((idx + offset) as i64 + 1);
331326
let source_type = semantic_model
332-
.infer_member_type(&table_type, &member_key)
327+
.infer_member_type(table_type, &member_key)
333328
.ok()?;
334329
match source_type {
335330
LuaType::Variadic(source_variadic) => {
@@ -343,12 +338,11 @@ fn check_table_last_variadic_type(
343338
semantic_model,
344339
range,
345340
Some(&source_type),
346-
&expr_type,
341+
expr_type,
347342
false,
348-
) {
349-
if result {
350-
return Some(true);
351-
}
343+
) && result
344+
{
345+
return Some(true);
352346
}
353347
}
354348
}
@@ -391,14 +385,14 @@ fn check_assign_type_mismatch(
391385
_ => {}
392386
}
393387

394-
let result = semantic_model.type_check_detail(&source_type, &value_type);
395-
if !result.is_ok() {
388+
let result = semantic_model.type_check_detail(source_type, value_type);
389+
if result.is_err() {
396390
add_type_check_diagnostic(
397391
context,
398392
semantic_model,
399393
range,
400-
&source_type,
401-
&value_type,
394+
source_type,
395+
value_type,
402396
result,
403397
);
404398
return Some(true);
@@ -416,7 +410,7 @@ fn add_type_check_diagnostic(
416410
) {
417411
let db = semantic_model.get_db();
418412
match result {
419-
Ok(_) => return,
413+
Ok(_) => (),
420414
Err(reason) => {
421415
let reason_message = match reason {
422416
TypeCheckFailReason::TypeNotMatchWithReason(reason) => reason,
@@ -429,8 +423,8 @@ fn add_type_check_diagnostic(
429423
range,
430424
t!(
431425
"Cannot assign `%{value}` to `%{source}`. %{reason}",
432-
value = humanize_lint_type(db, &value_type),
433-
source = humanize_lint_type(db, &source_type),
426+
value = humanize_lint_type(db, value_type),
427+
source = humanize_lint_type(db, source_type),
434428
reason = reason_message
435429
)
436430
.to_string(),

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

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,16 @@ fn check_call_in_async(
2626
let function_type = semantic_model.infer_call_expr_func(call_expr.clone(), None)?;
2727
let async_state = function_type.get_async_state();
2828

29-
if async_state == AsyncState::Async {
30-
let prefix_expr = call_expr.get_prefix_expr()?;
31-
match check_async_func_in_sync_call(semantic_model, call_expr) {
32-
Err(_) => {
33-
context.add_diagnostic(
34-
DiagnosticCode::AwaitInSync,
35-
prefix_expr.get_range(),
36-
t!("Async function can only be called in async function.").to_string(),
37-
None,
38-
);
39-
}
40-
Ok(()) => {}
41-
}
29+
if async_state == AsyncState::Async
30+
&& let Some(prefix_expr) = call_expr.get_prefix_expr()
31+
&& check_async_func_in_sync_call(semantic_model, call_expr).is_err()
32+
{
33+
context.add_diagnostic(
34+
DiagnosticCode::AwaitInSync,
35+
prefix_expr.get_range(),
36+
t!("Async function can only be called in async function.").to_string(),
37+
None,
38+
);
4239
}
4340

4441
Some(())
@@ -74,25 +71,21 @@ fn check_call_as_arg(
7471
let async_state = match &arg_type {
7572
LuaType::DocFunction(f) => f.get_async_state(),
7673
LuaType::Signature(sig) => {
77-
let signature = semantic_model.get_db().get_signature_index().get(&sig)?;
74+
let signature = semantic_model.get_db().get_signature_index().get(sig)?;
7875
signature.async_state
7976
}
8077
_ => continue,
8178
};
8279

83-
if async_state == AsyncState::Async {
84-
match check_async_func_in_sync_call(semantic_model, call_expr.clone()) {
85-
Err(_) => {
86-
context.add_diagnostic(
87-
DiagnosticCode::AwaitInSync,
88-
arg.get_range(),
89-
t!("Async function can only be called in async function.")
90-
.to_string(),
91-
None,
92-
);
93-
}
94-
Ok(()) => {}
95-
}
80+
if async_state == AsyncState::Async
81+
&& check_async_func_in_sync_call(semantic_model, call_expr.clone()).is_err()
82+
{
83+
context.add_diagnostic(
84+
DiagnosticCode::AwaitInSync,
85+
arg.get_range(),
86+
t!("Async function can only be called in async function.").to_string(),
87+
None,
88+
);
9689
}
9790
}
9891
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,13 @@ fn expand_type_recursive(
223223
visited.insert(typ.clone());
224224

225225
// 展开类型, 如果具有多种类型将尽量返回 union
226-
match get_real_type(db, &typ).unwrap_or(&typ) {
226+
match get_real_type(db, typ).unwrap_or(typ) {
227227
LuaType::Ref(id) | LuaType::Def(id) => {
228228
let type_decl = db.get_type_index().get_type_decl(id)?;
229-
if type_decl.is_enum() {
230-
if let Some(typ) = type_decl.get_enum_field_type(db) {
231-
return expand_type_recursive(db, &typ, visited);
232-
}
229+
if type_decl.is_enum()
230+
&& let Some(typ) = type_decl.get_enum_field_type(db)
231+
{
232+
return expand_type_recursive(db, &typ, visited);
233233
};
234234
}
235235
LuaType::Instance(inst) => {

0 commit comments

Comments
 (0)