mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-23 08:42:40 +00:00
add diagnostic for dangling dyn
This commit is contained in:
parent
9df88ff0f6
commit
afe6e5ba0f
3 changed files with 42 additions and 8 deletions
|
@ -340,17 +340,25 @@ fn validate_trait_object_fn_ptr_ret_ty(ty: ast::FnPtrType, errors: &mut Vec<Synt
|
||||||
|
|
||||||
fn validate_trait_object_ty(ty: ast::DynTraitType) -> Option<SyntaxError> {
|
fn validate_trait_object_ty(ty: ast::DynTraitType) -> Option<SyntaxError> {
|
||||||
let tbl = ty.type_bound_list()?;
|
let tbl = ty.type_bound_list()?;
|
||||||
|
let bounds_count = tbl.bounds().count();
|
||||||
|
|
||||||
if tbl.bounds().count() > 1 {
|
match bounds_count {
|
||||||
|
0 => Some(SyntaxError::new(
|
||||||
|
"At least one trait is required for an object type",
|
||||||
|
ty.syntax().text_range(),
|
||||||
|
)),
|
||||||
|
_ if bounds_count > 1 => {
|
||||||
let dyn_token = ty.dyn_token()?;
|
let dyn_token = ty.dyn_token()?;
|
||||||
let potential_parenthesis =
|
let preceding_token =
|
||||||
algo::skip_trivia_token(dyn_token.prev_token()?, Direction::Prev)?;
|
algo::skip_trivia_token(dyn_token.prev_token()?, Direction::Prev)?;
|
||||||
let kind = potential_parenthesis.kind();
|
|
||||||
if !matches!(kind, T!['('] | T![<] | T![=]) {
|
if !matches!(preceding_token.kind(), T!['('] | T![<] | T![=]) {
|
||||||
return Some(SyntaxError::new("ambiguous `+` in a type", ty.syntax().text_range()));
|
return Some(SyntaxError::new("ambiguous `+` in a type", ty.syntax().text_range()));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
None
|
None
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_macro_rules(mac: ast::MacroRules, errors: &mut Vec<SyntaxError>) {
|
fn validate_macro_rules(mac: ast::MacroRules, errors: &mut Vec<SyntaxError>) {
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
SOURCE_FILE@0..16
|
||||||
|
FN@0..16
|
||||||
|
FN_KW@0..2 "fn"
|
||||||
|
WHITESPACE@2..3 " "
|
||||||
|
NAME@3..4
|
||||||
|
IDENT@3..4 "f"
|
||||||
|
PARAM_LIST@4..13
|
||||||
|
L_PAREN@4..5 "("
|
||||||
|
PARAM@5..12
|
||||||
|
WILDCARD_PAT@5..6
|
||||||
|
UNDERSCORE@5..6 "_"
|
||||||
|
COLON@6..7 ":"
|
||||||
|
WHITESPACE@7..8 " "
|
||||||
|
REF_TYPE@8..12
|
||||||
|
AMP@8..9 "&"
|
||||||
|
DYN_TRAIT_TYPE@9..12
|
||||||
|
DYN_KW@9..12 "dyn"
|
||||||
|
TYPE_BOUND_LIST@12..12
|
||||||
|
R_PAREN@12..13 ")"
|
||||||
|
WHITESPACE@13..14 " "
|
||||||
|
BLOCK_EXPR@14..16
|
||||||
|
STMT_LIST@14..16
|
||||||
|
L_CURLY@14..15 "{"
|
||||||
|
R_CURLY@15..16 "}"
|
||||||
|
error 9..12: At least one trait is required for an object type
|
|
@ -0,0 +1 @@
|
||||||
|
fn f(_: &dyn) {}
|
Loading…
Add table
Add a link
Reference in a new issue