add diagnostic for unknown path based ink! attributes

This commit is contained in:
davidsemakula 2023-03-16 23:23:27 +03:00
parent 9495f66241
commit 76661003de
3 changed files with 25 additions and 5 deletions

View file

@ -14,7 +14,7 @@ It therefore implements the core functionality of this library.
Currently only 2 diagnostics that return a diagnostic model that includes an error/warning message, the text range to which the diagnostic applies and its severity are implemented:
1. an error diagnostic that detects when the `#[ink::contract]` attribute is applied to anything other than a `mod` item.
2. a warning diagnostic for unknown ink! macro attributes (e.g. `#[ink::xyz]`).
2. a warning diagnostic for unknown ink! macro attributes (e.g. `#[ink::xyz]`, `#[ink::abc::xyz]` e.t.c).
You can find its core implementations in the [diagnostics](./src/analysis/diagnostics.rs) submodule.

View file

@ -33,10 +33,10 @@ pub fn diagnostics(file: &SourceFile) -> Vec<Diagnostic> {
for attr in attrs {
if let Attribute::Ink(ink_attr) = Attribute::from(attr) {
let node = ink_attr.ast.syntax();
match ink_attr.kind {
// Validate ink! macro attributes
InkAttributeKind::Macro(ink_macro_kind) => {
let node = ink_attr.ast.syntax();
if ink_macro_kind == InkMacroAttributeKind::Unknown {
diagnostic_errors.push(Diagnostic {
message: format!("Unknown ink! attribute"),
@ -61,10 +61,18 @@ pub fn diagnostics(file: &SourceFile) -> Vec<Diagnostic> {
}
}
}
// Validate ink! macro attributes
InkAttributeKind::Arg(_) => {
// TODO: Validate ink! argument attributes
}
_ => (),
// Handle generic unknown ink! attributes
_ => {
diagnostic_errors.push(Diagnostic {
message: format!("Unknown ink! attribute"),
range: node.text_range(),
severity: Severity::Warning, // warning because it's possible ink-analyzer is just outdated
});
}
}
}
}

View file

@ -34,7 +34,7 @@ mod tests {
"#;
let diagnostics = Analysis.diagnostics(code);
assert_eq!(diagnostics.len(), 0);
assert!(diagnostics.is_empty());
}
#[test]
@ -48,7 +48,7 @@ mod tests {
"#;
let diagnostics = Analysis.diagnostics(code);
assert_eq!(diagnostics.len(), 0);
assert!(diagnostics.is_empty());
}
#[test]
@ -98,4 +98,16 @@ mod tests {
let diagnostics = Analysis.diagnostics(code);
assert_eq!(diagnostics.len(), 1);
}
#[test]
fn ink_unknown_path_attribute_fails() {
let code = r#"
#[ink::abc::xyz]
mod flipper {
}
"#;
let diagnostics = Analysis.diagnostics(code);
assert_eq!(diagnostics.len(), 1);
}
}