Skip to content

Commit 8c7e093

Browse files
committed
fix @as
1 parent 471f019 commit 8c7e093

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

crates/emmylua_code_analysis/src/compilation/analyzer/doc/type_ref_tags.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use emmylua_parser::{
2-
LuaAst, LuaAstNode, LuaAstToken, LuaDocDescriptionOwner, LuaDocTagAs, LuaDocTagCast,
2+
LuaAst, LuaAstNode, LuaAstToken, LuaBlock, LuaDocDescriptionOwner, LuaDocTagAs, LuaDocTagCast,
33
LuaDocTagModule, LuaDocTagOther, LuaDocTagOverload, LuaDocTagParam, LuaDocTagReturn,
44
LuaDocTagSee, LuaDocTagType, LuaExpr, LuaLocalName, LuaTokenKind, LuaVarExpr,
55
};
@@ -278,7 +278,16 @@ pub fn analyze_as(analyzer: &mut DocAnalyzer, tag: LuaDocTagAs) -> Option<()> {
278278
left_token = left_token.prev_token()?;
279279
}
280280

281-
let expr = LuaExpr::cast(left_token.parent()?)?;
281+
let mut ast_node = left_token.parent()?;
282+
loop {
283+
if LuaExpr::can_cast(ast_node.kind().into()) {
284+
break;
285+
} else if LuaBlock::can_cast(ast_node.kind().into()) {
286+
return None;
287+
}
288+
ast_node = ast_node.parent()?;
289+
}
290+
let expr = LuaExpr::cast(ast_node)?;
282291

283292
let file_id = analyzer.file_id;
284293
let in_filed_syntax_id = InFiled::new(file_id, expr.get_syntax_id());

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,24 @@ mod tests {
205205
"#
206206
));
207207
}
208+
209+
#[test]
210+
fn test_as_return_type() {
211+
let mut ws = VirtualWorkspace::new();
212+
213+
assert!(ws.check_code_for(
214+
DiagnosticCode::ReturnTypeMismatch,
215+
r#"
216+
local function dd()
217+
return "11231"
218+
end
219+
220+
---@return integer
221+
local function f()
222+
223+
return dd() ---@as integer
224+
end
225+
"#
226+
));
227+
}
208228
}

0 commit comments

Comments
 (0)