mirror of
https://github.com/oppiliappan/statix.git
synced 2024-08-17 05:00:24 +03:00
new lint: empty-let-in
This commit is contained in:
parent
2dacf12a3d
commit
a60429891f
20
Cargo.lock
generated
20
Cargo.lock
generated
@ -23,16 +23,6 @@ version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
||||
|
||||
[[package]]
|
||||
name = "bin"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"ariadne",
|
||||
"lib",
|
||||
"rnix",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cbitset"
|
||||
version = "0.2.0"
|
||||
@ -157,6 +147,16 @@ version = "0.1.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b203e79e90905594272c1c97c7af701533d42adaab0beb3859018e477d54a3b0"
|
||||
|
||||
[[package]]
|
||||
name = "statix"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"ariadne",
|
||||
"lib",
|
||||
"rnix",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.76"
|
||||
|
@ -32,7 +32,7 @@ impl Report {
|
||||
self
|
||||
}
|
||||
/// Add a diagnostic with a fix to this report
|
||||
pub fn suggest(mut self, at: TextRange, message: String, suggestion: Suggestion) -> Self {
|
||||
pub fn suggest<S: AsRef<str>>(mut self, at: TextRange, message: S, suggestion: Suggestion) -> Self {
|
||||
self.diagnostics.push(Diagnostic::suggest(at, message, suggestion));
|
||||
self
|
||||
}
|
||||
@ -54,8 +54,8 @@ impl Diagnostic {
|
||||
Self { at, message, suggestion: None }
|
||||
}
|
||||
/// Construct a diagnostic with a fix.
|
||||
pub fn suggest(at: TextRange, message: String, suggestion: Suggestion) -> Self {
|
||||
Self { at, message, suggestion: Some(suggestion) }
|
||||
pub fn suggest<S: AsRef<str>>(at: TextRange, message: S, suggestion: Suggestion) -> Self {
|
||||
Self { at, message: message.as_ref().into(), suggestion: Some(suggestion) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,4 +2,5 @@ use crate::lint_map;
|
||||
|
||||
lint_map! {
|
||||
bool_comparison,
|
||||
empty_let_in,
|
||||
}
|
||||
|
42
lib/src/lints/empty_let_in.rs
Normal file
42
lib/src/lints/empty_let_in.rs
Normal file
@ -0,0 +1,42 @@
|
||||
use crate::{Lint, Metadata, Report, Rule, Suggestion};
|
||||
|
||||
use if_chain::if_chain;
|
||||
use macros::lint;
|
||||
use rnix::{
|
||||
types::{LetIn, TypedNode,
|
||||
EntryHolder},
|
||||
NodeOrToken, SyntaxElement, SyntaxKind,
|
||||
};
|
||||
|
||||
#[lint(
|
||||
name = "empty let-in",
|
||||
note = "Useless let-in expression",
|
||||
code = 2,
|
||||
match_with = SyntaxKind::NODE_LET_IN
|
||||
)]
|
||||
struct EmptyLetIn;
|
||||
|
||||
impl Rule for EmptyLetIn {
|
||||
fn validate(&self, node: &SyntaxElement) -> Option<Report> {
|
||||
if_chain! {
|
||||
if let NodeOrToken::Node(let_in_node) = node;
|
||||
if let Some(let_in_expr) = LetIn::cast(let_in_node.clone());
|
||||
let entries = let_in_expr.entries();
|
||||
let inherits = let_in_expr.inherits();
|
||||
|
||||
if entries.count() == 0;
|
||||
if inherits.count() == 0;
|
||||
|
||||
if let Some(body) = let_in_expr.body();
|
||||
then {
|
||||
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)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user