Skip to content

Commit 6c98c39

Browse files
committed
diagnostic ParamTypeNotMatch: int 允许浮点数常量尾数为 .0 通过
1 parent 3115208 commit 6c98c39

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

crates/emmylua_code_analysis/src/db_index/type/humanize_type.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ pub fn humanize_type(db: &DbIndex, ty: &LuaType, level: RenderLevel) -> String {
4242
LuaType::Thread => "thread".to_string(),
4343
LuaType::Userdata => "userdata".to_string(),
4444
LuaType::IntegerConst(i) => i.to_string(),
45-
LuaType::FloatConst(f) => f.to_string(),
45+
LuaType::FloatConst(f) => {
46+
let s = f.to_string();
47+
// 如果字符串不包含小数点,添加 ".0"
48+
if !s.contains('.') {
49+
format!("{}.0", s)
50+
} else {
51+
s
52+
}
53+
}
4654
LuaType::TableConst(v) => {
4755
let member_owner = LuaMemberOwner::Element(v.clone());
4856
humanize_table_const_type(db, member_owner, level)

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn check_call_expr(
8585
}
8686
let result = semantic_model.type_check(&check_type, arg_type);
8787
if !result.is_ok() {
88-
add_type_check_diagnostic(
88+
try_add_diagnostic(
8989
context,
9090
semantic_model,
9191
*arg_ranges.get(idx)?,
@@ -110,7 +110,7 @@ fn check_variadic_param_match_args(
110110
for (arg_type, arg_range) in arg_types.iter().zip(arg_ranges.iter()) {
111111
let result = semantic_model.type_check(variadic_type, arg_type);
112112
if !result.is_ok() {
113-
add_type_check_diagnostic(
113+
try_add_diagnostic(
114114
context,
115115
semantic_model,
116116
*arg_range,
@@ -122,6 +122,33 @@ fn check_variadic_param_match_args(
122122
}
123123
}
124124

125+
fn try_add_diagnostic(
126+
context: &mut DiagnosticContext,
127+
semantic_model: &SemanticModel,
128+
range: TextRange,
129+
param_type: &LuaType,
130+
expr_type: &LuaType,
131+
result: TypeCheckResult,
132+
) {
133+
match (param_type, expr_type) {
134+
(LuaType::Integer, LuaType::FloatConst(f)) => {
135+
if f.fract() == 0.0 {
136+
return;
137+
}
138+
}
139+
_ => {}
140+
}
141+
142+
add_type_check_diagnostic(
143+
context,
144+
semantic_model,
145+
range,
146+
param_type,
147+
expr_type,
148+
result,
149+
);
150+
}
151+
125152
fn add_type_check_diagnostic(
126153
context: &mut DiagnosticContext,
127154
semantic_model: &SemanticModel,

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,4 +1004,19 @@ mod test {
10041004
"#
10051005
));
10061006
}
1007+
1008+
#[test]
1009+
fn test_int() {
1010+
let mut ws = VirtualWorkspace::new();
1011+
assert!(ws.check_code_for(
1012+
DiagnosticCode::ParamTypeNotMatch,
1013+
r#"
1014+
---@param count integer
1015+
local function loop_count(count)
1016+
end
1017+
1018+
loop_count(45 / 3)
1019+
"#
1020+
));
1021+
}
10071022
}

0 commit comments

Comments
 (0)