From 76661003de39dc8d265d4d73ad0aaadd6f968596 Mon Sep 17 00:00:00 2001 From: davidsemakula Date: Thu, 16 Mar 2023 23:23:27 +0300 Subject: [PATCH] add diagnostic for unknown path based ink! attributes --- README.md | 2 +- src/analysis/diagnostics.rs | 12 ++++++++++-- src/analysis/mod.rs | 16 ++++++++++++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8cefa7b..77749c3 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/analysis/diagnostics.rs b/src/analysis/diagnostics.rs index 504aeff..112615d 100644 --- a/src/analysis/diagnostics.rs +++ b/src/analysis/diagnostics.rs @@ -33,10 +33,10 @@ pub fn diagnostics(file: &SourceFile) -> Vec { 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 { } } } + // 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 + }); + } } } } diff --git a/src/analysis/mod.rs b/src/analysis/mod.rs index bb9117a..71e367e 100644 --- a/src/analysis/mod.rs +++ b/src/analysis/mod.rs @@ -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); + } }