From 1748d5f1546d2160ec8f949a56081403a3f9df62 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 31 Oct 2021 14:35:58 +0530 Subject: begin writing explainations --- lib/src/lib.rs | 30 +++++++++++++++++------------- lib/src/lints/bool_comparison.rs | 26 ++++++++++++++++++++++++-- lib/src/lints/collapsible_let_in.rs | 9 ++++----- lib/src/lints/empty_let_in.rs | 8 ++++---- lib/src/lints/empty_pattern.rs | 4 ++-- lib/src/lints/eta_reduction.rs | 4 ++-- lib/src/lints/legacy_let_syntax.rs | 4 ++-- lib/src/lints/manual_inherit.rs | 4 ++-- lib/src/lints/manual_inherit_from.rs | 4 ++-- lib/src/lints/redundant_pattern_bind.rs | 4 ++-- lib/src/lints/unquoted_splice.rs | 4 ++-- lib/src/lints/useless_parens.rs | 10 +++++----- 12 files changed, 68 insertions(+), 43 deletions(-) diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 196cbf8..e6bfa6c 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -226,25 +226,29 @@ pub trait Rule { /// Contains information about the lint itself. Do not implement manually, /// look at the `lint` attribute macro instead for implementing rules pub trait Metadata { - fn name() -> &'static str - where - Self: Sized; - fn note() -> &'static str - where - Self: Sized; - fn code() -> u32 - where - Self: Sized; - fn report() -> Report - where - Self: Sized; + fn name(&self) -> &'static str; + fn note(&self) -> &'static str; + fn code(&self) -> u32; + fn report(&self) -> Report; fn match_with(&self, with: &SyntaxKind) -> bool; fn match_kind(&self) -> Vec; } +/// Contains offline explaination for each lint +/// The `lint` macro scans nearby doc comments for +/// explainations and derives this trait. +/// +/// FIXME: the lint macro does way too much, maybe +/// split it into smaller macros. +pub trait Explain { + fn explaination(&self) -> &'static str { + "no explaination found" + } +} + /// Combines Rule and Metadata, do not implement manually, this is derived by /// the `lint` macro. -pub trait Lint: Metadata + Rule + Send + Sync {} +pub trait Lint: Metadata + Explain + Rule + Send + Sync {} /// Helper utility to take lints from modules and insert them into a map for efficient /// access. Mapping is from a SyntaxKind to a list of lints that apply on that Kind. diff --git a/lib/src/lints/bool_comparison.rs b/lib/src/lints/bool_comparison.rs index 0b5733b..6636faf 100644 --- a/lib/src/lints/bool_comparison.rs +++ b/lib/src/lints/bool_comparison.rs @@ -1,4 +1,4 @@ -use crate::{make, Lint, Metadata, Report, Rule, Suggestion}; +use crate::{make, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -7,6 +7,28 @@ use rnix::{ NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode, }; +/// What it does +/// ------------ +/// Checks for expressions of the form x == true, x != true and +/// suggests using the variable directly. +/// +/// Why is this bad? +/// ---------------- +/// Unnecessary code. +/// +/// Example +/// -------- +/// Instead of checking the value of x: +/// +/// if x == true +/// then 0 +/// else 1 +/// +/// Use x directly: +/// +/// if x +/// then 0 +/// else 1 #[lint( name = "bool_comparison", note = "Unnecessary comparison with boolean", @@ -71,7 +93,7 @@ impl Rule for BoolComparison { non_bool_side, bool_side ); - Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) + Some(self.report().suggest(at, message, Suggestion::new(at, replacement))) } else { None } diff --git a/lib/src/lints/collapsible_let_in.rs b/lib/src/lints/collapsible_let_in.rs index 878d218..cb76c37 100644 --- a/lib/src/lints/collapsible_let_in.rs +++ b/lib/src/lints/collapsible_let_in.rs @@ -1,12 +1,12 @@ -use crate::{make, Lint, Metadata, Report, Rule, Suggestion}; +use crate::{make, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; -use rowan::Direction; use rnix::{ types::{LetIn, TypedNode}, - NodeOrToken, SyntaxElement, SyntaxKind, TextRange + NodeOrToken, SyntaxElement, SyntaxKind, TextRange, }; +use rowan::Direction; #[lint( name = "collapsible let in", @@ -47,7 +47,7 @@ impl Rule for CollapsibleLetIn { let replacement = make::empty().node().clone(); Some( - Self::report() + self.report() .diagnostic(first_annotation, first_message) .suggest(second_annotation, second_message, Suggestion::new(replacement_at, replacement)) ) @@ -57,4 +57,3 @@ impl Rule for CollapsibleLetIn { } } } - diff --git a/lib/src/lints/empty_let_in.rs b/lib/src/lints/empty_let_in.rs index aae1377..4e0887d 100644 --- a/lib/src/lints/empty_let_in.rs +++ b/lib/src/lints/empty_let_in.rs @@ -1,12 +1,13 @@ -use crate::{Lint, Metadata, Report, Rule, Suggestion}; +use crate::{Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; use rnix::{ - types::{LetIn, TypedNode, EntryHolder}, + types::{EntryHolder, LetIn, TypedNode}, NodeOrToken, SyntaxElement, SyntaxKind, }; +/// empty let-in found #[lint( name = "empty let-in", note = "Useless let-in expression", @@ -31,11 +32,10 @@ impl Rule for EmptyLetIn { let at = node.text_range(); let replacement = body; let message = "This let-in expression has no entries"; - Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) + Some(self.report().suggest(at, message, Suggestion::new(at, replacement))) } else { None } } } } - diff --git a/lib/src/lints/empty_pattern.rs b/lib/src/lints/empty_pattern.rs index 6fb7558..ef47c69 100644 --- a/lib/src/lints/empty_pattern.rs +++ b/lib/src/lints/empty_pattern.rs @@ -1,4 +1,4 @@ -use crate::{make, Lint, Metadata, Report, Rule, Suggestion}; +use crate::{make, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -28,7 +28,7 @@ impl Rule for EmptyPattern { let at = node.text_range(); let message = "This pattern is empty, use `_` instead"; let replacement = make::ident("_").node().clone(); - Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) + Some(self.report().suggest(at, message, Suggestion::new(at, replacement))) } else { None } diff --git a/lib/src/lints/eta_reduction.rs b/lib/src/lints/eta_reduction.rs index 79a5101..b329ae8 100644 --- a/lib/src/lints/eta_reduction.rs +++ b/lib/src/lints/eta_reduction.rs @@ -1,4 +1,4 @@ -use crate::{Lint, Metadata, Report, Rule, Suggestion}; +use crate::{Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -43,7 +43,7 @@ impl Rule for EtaReduction { "Found eta-reduction: `{}`", replacement.text().to_string() ); - Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) + Some(self.report().suggest(at, message, Suggestion::new(at, replacement))) } else { None } diff --git a/lib/src/lints/legacy_let_syntax.rs b/lib/src/lints/legacy_let_syntax.rs index 2087e27..8b37df8 100644 --- a/lib/src/lints/legacy_let_syntax.rs +++ b/lib/src/lints/legacy_let_syntax.rs @@ -1,4 +1,4 @@ -use crate::{make, Lint, Metadata, Report, Rule, Suggestion}; +use crate::{make, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -36,7 +36,7 @@ impl Rule for ManualInherit { let message = "Prefer `rec` over undocumented `let` syntax"; let replacement = selected.node().clone(); - Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) + Some(self.report().suggest(at, message, Suggestion::new(at, replacement))) } else { None } diff --git a/lib/src/lints/manual_inherit.rs b/lib/src/lints/manual_inherit.rs index 0a6933c..1c10cbf 100644 --- a/lib/src/lints/manual_inherit.rs +++ b/lib/src/lints/manual_inherit.rs @@ -1,4 +1,4 @@ -use crate::{make, Lint, Metadata, Report, Rule, Suggestion}; +use crate::{make, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -35,7 +35,7 @@ impl Rule for ManualInherit { let at = node.text_range(); let replacement = make::inherit_stmt(&[key]).node().clone(); let message = "This assignment is better written with `inherit`"; - Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) + Some(self.report().suggest(at, message, Suggestion::new(at, replacement))) } else { None } diff --git a/lib/src/lints/manual_inherit_from.rs b/lib/src/lints/manual_inherit_from.rs index 794aaf9..146d55b 100644 --- a/lib/src/lints/manual_inherit_from.rs +++ b/lib/src/lints/manual_inherit_from.rs @@ -1,4 +1,4 @@ -use crate::{make, Lint, Metadata, Report, Rule, Suggestion}; +use crate::{make, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -40,7 +40,7 @@ impl Rule for ManualInherit { make::inherit_from_stmt(set, &[key]).node().clone() }; let message = "This assignment is better written with `inherit`"; - Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) + Some(self.report().suggest(at, message, Suggestion::new(at, replacement))) } else { None } diff --git a/lib/src/lints/redundant_pattern_bind.rs b/lib/src/lints/redundant_pattern_bind.rs index aebc549..ad1aba8 100644 --- a/lib/src/lints/redundant_pattern_bind.rs +++ b/lib/src/lints/redundant_pattern_bind.rs @@ -1,4 +1,4 @@ -use crate::{Lint, Metadata, Report, Rule, Suggestion}; +use crate::{Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -32,7 +32,7 @@ impl Rule for RedundantPatternBind { let at = node.text_range(); let message = format!("This pattern bind is redundant, use `{}` instead", ident.as_str()); let replacement = ident.node().clone(); - Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) + Some(self.report().suggest(at, message, Suggestion::new(at, replacement))) } else { None } diff --git a/lib/src/lints/unquoted_splice.rs b/lib/src/lints/unquoted_splice.rs index 4d1ed69..2831c1b 100644 --- a/lib/src/lints/unquoted_splice.rs +++ b/lib/src/lints/unquoted_splice.rs @@ -1,4 +1,4 @@ -use crate::{make, Lint, Metadata, Report, Rule, Suggestion}; +use crate::{make, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -24,7 +24,7 @@ impl Rule for UnquotedSplice { let at = node.text_range(); let replacement = make::quote(&node).node().clone(); let message = "Consider quoting this splice expression"; - Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) + Some(self.report().suggest(at, message, Suggestion::new(at, replacement))) } else { None } diff --git a/lib/src/lints/useless_parens.rs b/lib/src/lints/useless_parens.rs index 2d6ba8f..9dfeabb 100644 --- a/lib/src/lints/useless_parens.rs +++ b/lib/src/lints/useless_parens.rs @@ -1,9 +1,9 @@ -use crate::{Lint, Metadata, Report, Rule, Suggestion, Diagnostic}; +use crate::{Diagnostic, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; use rnix::{ - types::{ParsedType, KeyValue, Paren, TypedNode, Wrapper}, + types::{KeyValue, Paren, ParsedType, TypedNode, Wrapper}, NodeOrToken, SyntaxElement, SyntaxKind, }; @@ -27,7 +27,7 @@ impl Rule for UselessParens { if let Some(diagnostic) = do_thing(parsed_type_node); then { - let mut report = Self::report(); + let mut report = self.report(); report.diagnostics.push(diagnostic); Some(report) } else { @@ -79,7 +79,7 @@ fn do_thing(parsed_type_node: ParsedType) -> Option { if let Some(parsed_inner) = ParsedType::cast(inner_node); if matches!( parsed_inner, - ParsedType::List(_) + ParsedType::List(_) | ParsedType::Paren(_) | ParsedType::Str(_) | ParsedType::AttrSet(_) @@ -95,6 +95,6 @@ fn do_thing(parsed_type_node: ParsedType) -> Option { None } }, - _ => None + _ => None, } } -- cgit v1.2.3