feat: don't wavy-underline the whole for loop

This commit is contained in:
Aleksey Kladov 2023-04-24 11:31:52 +01:00
parent 0289dfa261
commit 774575106c

View file

@ -1,6 +1,7 @@
use either::Either; use either::Either;
use hir::{db::ExpandDatabase, ClosureStyle, HirDisplay, InFile, Type}; use hir::{db::ExpandDatabase, ClosureStyle, HirDisplay, InFile, Type};
use ide_db::{famous_defs::FamousDefs, source_change::SourceChange}; use ide_db::{famous_defs::FamousDefs, source_change::SourceChange};
use stdx::never;
use syntax::{ use syntax::{
ast::{self, BlockExpr, ExprStmt}, ast::{self, BlockExpr, ExprStmt},
AstNode, AstPtr, AstNode, AstPtr,
@ -15,15 +16,29 @@ use crate::{adjusted_display_range, fix, Assist, Diagnostic, DiagnosticsContext}
// the expected type. // the expected type.
pub(crate) fn type_mismatch(ctx: &DiagnosticsContext<'_>, d: &hir::TypeMismatch) -> Diagnostic { pub(crate) fn type_mismatch(ctx: &DiagnosticsContext<'_>, d: &hir::TypeMismatch) -> Diagnostic {
let display_range = match &d.expr_or_pat { let display_range = match &d.expr_or_pat {
Either::Left(expr) => adjusted_display_range::<ast::BlockExpr>( Either::Left(expr) => {
ctx, adjusted_display_range::<ast::Expr>(ctx, expr.clone().map(|it| it.into()), &|expr| {
expr.clone().map(|it| it.into()), if !expr.is_block_like() {
&|block| { return None;
let r_curly_range = block.stmt_list()?.r_curly_token()?.text_range(); }
let salient_token_range = match expr {
ast::Expr::IfExpr(it) => it.if_token()?.text_range(),
ast::Expr::LoopExpr(it) => it.loop_token()?.text_range(),
ast::Expr::ForExpr(it) => it.for_token()?.text_range(),
ast::Expr::WhileExpr(it) => it.while_token()?.text_range(),
ast::Expr::BlockExpr(it) => it.stmt_list()?.r_curly_token()?.text_range(),
ast::Expr::MatchExpr(it) => it.match_token()?.text_range(),
_ => {
never!();
return None;
}
};
cov_mark::hit!(type_mismatch_on_block); cov_mark::hit!(type_mismatch_on_block);
Some(r_curly_range) Some(salient_token_range)
}, })
), }
Either::Right(pat) => { Either::Right(pat) => {
ctx.sema.diagnostics_display_range(pat.clone().map(|it| it.into())).range ctx.sema.diagnostics_display_range(pat.clone().map(|it| it.into())).range
} }
@ -620,6 +635,10 @@ fn f() -> i32 {
let _ = x + y; let _ = x + y;
} }
//^ error: expected i32, found () //^ error: expected i32, found ()
fn h() -> i32 {
while true {}
} //^^^^^ error: expected i32, found ()
"#, "#,
); );
} }