Skip to content

Commit a50c8db

Browse files
authored
Merge pull request #385 from ribru17/unnecessary_if
feat: diagnostics for unnecessary "if" conditions
2 parents 640a7cd + 86d3fa6 commit a50c8db

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

crates/emmylua_code_analysis/locales/lint.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ Cannot use `...` outside a vararg function.:
213213
en: 'Impossible assert: this expression is always falsy; prefer `error()`'
214214
zh_CN: '不可能的断言: 该表达式始终为假;建议使用 `error()`'
215215
zh_HK: '不可能的斷言: 該表達式始終為假;建議使用 `error()`'
216+
'Unnecessary `if` statement: this condition is always truthy':
217+
en: 'Unnecessary `if` statement: this condition is always truthy'
218+
zh_CN: '不必要的 `if` 语句:此条件始终为真'
219+
zh_HK: '不必要的 `if` 陳述式:此條件始終為真'
220+
'Impossible `if` statement: this condition is always falsy':
221+
en: 'Impossible `if` statement: this condition is always falsy'
222+
zh_CN: '不可能的 `if` 语句:此条件始终为假'
223+
zh_HK: '不可能的 `if` 陳述式:此條件始終為假'
216224
"`...` should be the last arg.":
217225
en: "`...` should be the last arg."
218226
zh_CN: "`...`必须是最后一个参数。"

crates/emmylua_code_analysis/resources/schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,13 @@
466466
"enum": [
467467
"unnecessary-assert"
468468
]
469+
},
470+
{
471+
"description": "unnecessary-if",
472+
"type": "string",
473+
"enum": [
474+
"unnecessary-if"
475+
]
469476
}
470477
]
471478
},

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod unbalanced_assignments;
2424
mod undefined_doc_param;
2525
mod undefined_global;
2626
mod unnecessary_assert;
27+
mod unnecessary_if;
2728
mod unused;
2829

2930
use emmylua_parser::{
@@ -65,6 +66,7 @@ pub fn check_file(context: &mut DiagnosticContext, semantic_model: &SemanticMode
6566
run_check::<deprecated::DeprecatedChecker>(context, semantic_model);
6667
run_check::<undefined_global::UndefinedGlobalChecker>(context, semantic_model);
6768
run_check::<unnecessary_assert::UnnecessaryAssertChecker>(context, semantic_model);
69+
run_check::<unnecessary_if::UnnecessaryIfChecker>(context, semantic_model);
6870
run_check::<access_invisible::AccessInvisibleChecker>(context, semantic_model);
6971
run_check::<local_const_reassign::LocalConstReassignChecker>(context, semantic_model);
7072
run_check::<discard_returns::DiscardReturnsChecker>(context, semantic_model);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use emmylua_parser::{LuaAstNode, LuaExpr, LuaIfStat};
2+
3+
use crate::{DiagnosticCode, LuaType, SemanticModel};
4+
5+
use super::{Checker, DiagnosticContext};
6+
7+
pub struct UnnecessaryIfChecker;
8+
9+
impl Checker for UnnecessaryIfChecker {
10+
const CODES: &[DiagnosticCode] = &[DiagnosticCode::UnnecessaryIf];
11+
12+
fn check(context: &mut DiagnosticContext, semantic_model: &SemanticModel) {
13+
let root = semantic_model.get_root().clone();
14+
for if_statement in root.descendants::<LuaIfStat>() {
15+
if let Some(condition) = if_statement.get_condition_expr() {
16+
check_condition(context, semantic_model, condition);
17+
}
18+
for clause in if_statement.get_else_if_clause_list() {
19+
if let Some(condition) = clause.get_condition_expr() {
20+
check_condition(context, semantic_model, condition);
21+
}
22+
}
23+
}
24+
}
25+
}
26+
27+
fn check_condition(
28+
context: &mut DiagnosticContext,
29+
semantic_model: &SemanticModel,
30+
condition: LuaExpr,
31+
) -> Option<()> {
32+
let expr_type = semantic_model.infer_expr(condition.clone()).ok()?;
33+
34+
if expr_type.is_always_truthy() {
35+
context.add_diagnostic(
36+
DiagnosticCode::UnnecessaryIf,
37+
condition.get_range(),
38+
t!("Unnecessary `if` statement: this condition is always truthy").to_string(),
39+
None,
40+
);
41+
} else if expr_type.is_always_falsy() {
42+
context.add_diagnostic(
43+
DiagnosticCode::UnnecessaryIf,
44+
condition.get_range(),
45+
t!("Impossible `if` statement: this condition is always falsy").to_string(),
46+
None,
47+
);
48+
}
49+
Some(())
50+
}

crates/emmylua_code_analysis/src/diagnostic/lua_diagnostic_code.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ pub enum DiagnosticCode {
9090
UnbalancedAssignments,
9191
/// unnecessary-assert
9292
UnnecessaryAssert,
93+
/// unnecessary-if
94+
UnnecessaryIf,
9395

9496
#[serde(other)]
9597
None,

0 commit comments

Comments
 (0)