diff --git a/lib/src/lib.rs b/lib/src/lib.rs index b285738..e4849b7 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -179,20 +179,42 @@ impl Serialize for Diagnostic { } } +#[derive(Debug)] +pub enum Replacement { + Empty, + SyntaxElement(SyntaxElement), +} + /// Suggested fix for a diagnostic, the fix is provided as a syntax element. /// Look at `make.rs` to construct fixes. #[derive(Debug)] pub struct Suggestion { pub at: TextRange, - pub fix: SyntaxElement, + pub fix: Replacement, +} + +impl std::fmt::Display for Replacement { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Replacement::Empty => Ok(()), + Replacement::SyntaxElement(syntax_element) => write!(f, "{syntax_element}"), + } + } } impl Suggestion { - /// Construct a suggestion. - pub fn new(at: TextRange, fix: impl Into) -> Self { + #[must_use] + pub fn with_replacement(at: TextRange, fix: impl Into) -> Self { + Self { + at, + fix: Replacement::SyntaxElement(fix.into()), + } + } + #[must_use] + pub fn with_empty(at: TextRange) -> Self { Self { at, - fix: fix.into(), + fix: Replacement::Empty, } } /// Apply a suggestion to a source file diff --git a/lib/src/lints/bool_comparison.rs b/lib/src/lints/bool_comparison.rs index 32daedf..f3b3b34 100644 --- a/lib/src/lints/bool_comparison.rs +++ b/lib/src/lints/bool_comparison.rs @@ -86,7 +86,7 @@ impl Rule for BoolComparison { Some(self.report().suggest( at, format!("Comparing `{non_bool_side}` with boolean literal `{bool_side}`"), - Suggestion::new(at, replacement), + Suggestion::with_replacement(at, replacement), )) } } diff --git a/lib/src/lints/bool_simplification.rs b/lib/src/lints/bool_simplification.rs index aa8fef9..9f8395d 100644 --- a/lib/src/lints/bool_simplification.rs +++ b/lib/src/lints/bool_simplification.rs @@ -59,7 +59,7 @@ impl Rule for BoolSimplification { let replacement = make::binary(&lhs, "!=", &rhs).node().clone(); Some( self.report() - .suggest(at, message, Suggestion::new(at, replacement)), + .suggest(at, message, Suggestion::with_replacement(at, replacement)), ) } } diff --git a/lib/src/lints/collapsible_let_in.rs b/lib/src/lints/collapsible_let_in.rs index 6939652..f633593 100644 --- a/lib/src/lints/collapsible_let_in.rs +++ b/lib/src/lints/collapsible_let_in.rs @@ -1,4 +1,4 @@ -use crate::{Metadata, Report, Rule, Suggestion, make, session::SessionInfo}; +use crate::{Metadata, Report, Rule, Suggestion, session::SessionInfo}; use macros::lint; use rnix::{ @@ -73,7 +73,6 @@ impl Rule for CollapsibleLetIn { .end(); TextRange::new(start, end) }; - let replacement = make::empty().node().clone(); Some( self.report() @@ -81,7 +80,7 @@ impl Rule for CollapsibleLetIn { .suggest( second_annotation, second_message, - Suggestion::new(replacement_at, replacement), + Suggestion::with_empty(replacement_at), ), ) } diff --git a/lib/src/lints/empty_inherit.rs b/lib/src/lints/empty_inherit.rs index ef93448..2632e25 100644 --- a/lib/src/lints/empty_inherit.rs +++ b/lib/src/lints/empty_inherit.rs @@ -1,4 +1,4 @@ -use crate::{Metadata, Report, Rule, Suggestion, make, session::SessionInfo, utils}; +use crate::{Metadata, Report, Rule, Suggestion, session::SessionInfo, utils}; use macros::lint; use rnix::{ @@ -44,12 +44,11 @@ impl Rule for EmptyInherit { } let at = node.text_range(); - let replacement = make::empty().node().clone(); let replacement_at = utils::with_preceeding_whitespace(node); let message = "Remove this empty `inherit` statement"; Some( self.report() - .suggest(at, message, Suggestion::new(replacement_at, replacement)), + .suggest(at, message, Suggestion::with_empty(replacement_at)), ) } } diff --git a/lib/src/lints/empty_let_in.rs b/lib/src/lints/empty_let_in.rs index 4624920..cce5cef 100644 --- a/lib/src/lints/empty_let_in.rs +++ b/lib/src/lints/empty_let_in.rs @@ -54,7 +54,7 @@ impl Rule for EmptyLetIn { self.report().diagnostic(at, message) } else { self.report() - .suggest(at, message, Suggestion::new(at, replacement)) + .suggest(at, message, Suggestion::with_replacement(at, replacement)) }) } else { None diff --git a/lib/src/lints/empty_list_concat.rs b/lib/src/lints/empty_list_concat.rs index eebea08..fe6624a 100644 --- a/lib/src/lints/empty_list_concat.rs +++ b/lib/src/lints/empty_list_concat.rs @@ -56,7 +56,7 @@ impl Rule for EmptyListConcat { Some( self.report() - .suggest(at, message, Suggestion::new(at, empty_array)), + .suggest(at, message, Suggestion::with_replacement(at, empty_array)), ) } } diff --git a/lib/src/lints/empty_pattern.rs b/lib/src/lints/empty_pattern.rs index a4f0275..f8dc716 100644 --- a/lib/src/lints/empty_pattern.rs +++ b/lib/src/lints/empty_pattern.rs @@ -65,7 +65,10 @@ impl Rule for EmptyPattern { Some(self.report().suggest( pattern.node().text_range(), "This pattern is empty, use `_` instead", - Suggestion::new(pattern.node().text_range(), make::ident("_").node().clone()), + Suggestion::with_replacement( + pattern.node().text_range(), + make::ident("_").node().clone(), + ), )) } } diff --git a/lib/src/lints/eta_reduction.rs b/lib/src/lints/eta_reduction.rs index fcaebd9..3511d98 100644 --- a/lib/src/lints/eta_reduction.rs +++ b/lib/src/lints/eta_reduction.rs @@ -69,7 +69,7 @@ impl Rule for EtaReduction { let message = format!("Found eta-reduction: `{}`", replacement.text()); Some( self.report() - .suggest(at, message, Suggestion::new(at, replacement)), + .suggest(at, message, Suggestion::with_replacement(at, replacement)), ) } } diff --git a/lib/src/lints/legacy_let_syntax.rs b/lib/src/lints/legacy_let_syntax.rs index 4028cc1..7e4b96b 100644 --- a/lib/src/lints/legacy_let_syntax.rs +++ b/lib/src/lints/legacy_let_syntax.rs @@ -62,7 +62,7 @@ impl Rule for ManualInherit { Some( self.report() - .suggest(at, message, Suggestion::new(at, replacement)), + .suggest(at, message, Suggestion::with_replacement(at, replacement)), ) } else { None diff --git a/lib/src/lints/manual_inherit.rs b/lib/src/lints/manual_inherit.rs index 8fd9fbf..7da7518 100644 --- a/lib/src/lints/manual_inherit.rs +++ b/lib/src/lints/manual_inherit.rs @@ -65,7 +65,7 @@ impl Rule for ManualInherit { Some(self.report().suggest( node.text_range(), "This assignment is better written with `inherit`", - Suggestion::new(node.text_range(), replacement), + Suggestion::with_replacement(node.text_range(), replacement), )) } } diff --git a/lib/src/lints/manual_inherit_from.rs b/lib/src/lints/manual_inherit_from.rs index f1d40fc..17fd0b4 100644 --- a/lib/src/lints/manual_inherit_from.rs +++ b/lib/src/lints/manual_inherit_from.rs @@ -70,7 +70,7 @@ impl Rule for ManualInherit { Some(self.report().suggest( at, "This assignment is better written with `inherit`", - Suggestion::new(at, replacement), + Suggestion::with_replacement(at, replacement), )) } } diff --git a/lib/src/lints/redundant_pattern_bind.rs b/lib/src/lints/redundant_pattern_bind.rs index 32eb8b2..0f4c82f 100644 --- a/lib/src/lints/redundant_pattern_bind.rs +++ b/lib/src/lints/redundant_pattern_bind.rs @@ -62,7 +62,7 @@ impl Rule for RedundantPatternBind { Some( self.report() - .suggest(at, message, Suggestion::new(at, replacement)), + .suggest(at, message, Suggestion::with_replacement(at, replacement)), ) } } diff --git a/lib/src/lints/unquoted_uri.rs b/lib/src/lints/unquoted_uri.rs index 319b7b5..d2c4e4c 100644 --- a/lib/src/lints/unquoted_uri.rs +++ b/lib/src/lints/unquoted_uri.rs @@ -56,7 +56,7 @@ impl Rule for UnquotedUri { let message = "Consider quoting this URI expression"; Some( self.report() - .suggest(at, message, Suggestion::new(at, replacement)), + .suggest(at, message, Suggestion::with_replacement(at, replacement)), ) } } diff --git a/lib/src/lints/useless_has_attr.rs b/lib/src/lints/useless_has_attr.rs index 1274c9c..4abd8e6 100644 --- a/lib/src/lints/useless_has_attr.rs +++ b/lib/src/lints/useless_has_attr.rs @@ -80,7 +80,7 @@ impl Rule for UselessHasAttr { let message = format!("Consider using `{replacement}` instead of this `if` expression"); Some( self.report() - .suggest(at, message, Suggestion::new(at, replacement)), + .suggest(at, message, Suggestion::with_replacement(at, replacement)), ) } } diff --git a/lib/src/lints/useless_parens.rs b/lib/src/lints/useless_parens.rs index 2f65540..ca5ddbd 100644 --- a/lib/src/lints/useless_parens.rs +++ b/lib/src/lints/useless_parens.rs @@ -59,7 +59,7 @@ impl Rule for UselessParens { Diagnostic::suggest( value_range, "Useless parentheses around value in binding", - Suggestion::new(value_range, Paren::cast(value_node)?.inner()?), + Suggestion::with_replacement(value_range, Paren::cast(value_node)?.inner()?), ) } ParsedType::LetIn(let_in) => { @@ -68,7 +68,7 @@ impl Rule for UselessParens { Diagnostic::suggest( body_range, "Useless parentheses around body of `let` expression", - Suggestion::new(body_range, Paren::cast(body_node)?.inner()?), + Suggestion::with_replacement(body_range, Paren::cast(body_node)?.inner()?), ) } ParsedType::Paren(paren_expr) => { @@ -104,7 +104,7 @@ impl Rule for UselessParens { Diagnostic::suggest( paren_expr_range, "Useless parentheses around primitive expression", - Suggestion::new(paren_expr_range, parsed_inner.node().clone()), + Suggestion::with_replacement(paren_expr_range, parsed_inner.node().clone()), ) } _ => return None, diff --git a/lib/src/make.rs b/lib/src/make.rs index 9af1ad8..5c4f888 100644 --- a/lib/src/make.rs +++ b/lib/src/make.rs @@ -80,10 +80,6 @@ pub fn ident(text: &str) -> types::Ident { ast_from_text(text) } -pub fn empty() -> types::Root { - ast_from_text("") -} - // LATER: make `op` strongly typed here pub fn binary(lhs: &SyntaxNode, op: &str, rhs: &SyntaxNode) -> types::BinOp { ast_from_text(&format!("{lhs} {op} {rhs}"))