Something went wrong. Try again.
Lints and suggestions for the Nix programming language forked from oppi.li/statix
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307#![recursion_limit = "1024"]mod lints;mod make;mod utils;
pub use lints::LINTS;
use rnix::{SyntaxElement, SyntaxKind, TextRange, parser::ParseError};use std::{convert::Into, default::Default};
#[cfg(feature = "json-out")]use serde::{ Serialize, ser::{SerializeStruct, Serializer},};
#[derive(Debug, Default)]#[cfg_attr(feature = "json-out", derive(Serialize))]pub enum Severity { #[default] Warn, Error, Hint,}
/// Report generated by a lint#[derive(Debug, Default)]#[cfg_attr(feature = "json-out", derive(Serialize))]pub struct Report { /// General information about this lint and where it applies. pub note: &'static str, /// An error code to uniquely identify this lint pub code: u32, /// Report severity level pub severity: Severity, /// Collection of diagnostics raised by this lint pub diagnostics: Vec<Diagnostic>,}
impl Report { /// Construct a report. Do not invoke `Report::new` manually, see `lint` macro #[must_use] pub fn new(note: &'static str, code: u32) -> Self { Self { note, code, ..Default::default() } } /// Add a diagnostic to this report #[allow(clippy::return_self_not_must_use)] pub fn diagnostic<S: AsRef<str>>(mut self, at: TextRange, message: S) -> Self { self.diagnostics.push(Diagnostic::new(at, message)); self } /// Add a diagnostic with a fix to this report #[allow(clippy::return_self_not_must_use)] pub fn suggest<S: AsRef<str>>( mut self, at: TextRange, message: S, suggestion: Suggestion, ) -> Self { self.diagnostics .push(Diagnostic::suggest(at, message, suggestion)); self } /// Set severity level #[allow(clippy::return_self_not_must_use, clippy::must_use_candidate)] pub fn severity(mut self, severity: Severity) -> Self { self.severity = severity; self } /// A range that encompasses all the suggestions provided in this report pub fn total_suggestion_range(&self) -> Option<TextRange> { self.diagnostics .iter() .filter_map(|d| Some(d.suggestion.as_ref()?.at)) .reduce(rnix::TextRange::cover) } /// A range that encompasses all the diagnostics provided in this report pub fn total_diagnostic_range(&self) -> Option<TextRange> { self.diagnostics .iter() .map(|d| d.at) .reduce(rnix::TextRange::cover) } /// Unsafe but handy replacement for above #[must_use] pub fn range(&self) -> TextRange { self.total_suggestion_range().unwrap() } /// Apply all diagnostics. Assumption: diagnostics do not overlap pub fn apply(&self, src: &mut String) { for d in &self.diagnostics { d.apply(src); } } /// Create a report out of a parse error #[must_use] pub fn from_parse_err(err: &ParseError) -> Self { let at = match err { ParseError::Unexpected(at) | ParseError::UnexpectedExtra(at) | ParseError::UnexpectedWanted(_, at, _) | ParseError::UnexpectedDoubleBind(at) | ParseError::DuplicatedArgs(at, _) => at, ParseError::UnexpectedEOF | ParseError::UnexpectedEOFWanted(_) => { &TextRange::empty(0u32.into()) } _ => panic!("report a bug, pepper forgot to handle a parse error"), }; let mut message = err.to_string(); message .as_mut_str() .get_mut(0..1) .unwrap() .make_ascii_uppercase(); Self::new("Syntax error", 0) .diagnostic(*at, message) .severity(Severity::Error) }}
/// Mapping from a bytespan to an error message./// Can optionally suggest a fix.#[derive(Debug)]pub struct Diagnostic { pub at: TextRange, pub message: String, pub suggestion: Option<Suggestion>,}
impl Diagnostic { /// Construct a diagnostic. pub fn new<S: AsRef<str>>(at: TextRange, message: S) -> Self { Self { at, message: message.as_ref().into(), suggestion: None, } } /// Construct a diagnostic with a fix. pub fn suggest<S: AsRef<str>>(at: TextRange, message: S, suggestion: Suggestion) -> Self { Self { at, message: message.as_ref().into(), suggestion: Some(suggestion), } } /// Apply a diagnostic to a source file pub fn apply(&self, src: &mut String) { if let Some(s) = &self.suggestion { s.apply(src); } }}
#[cfg(feature = "json-out")]impl Serialize for Diagnostic { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { let mut s = serializer.serialize_struct("Diagnostic", 3)?; let at = { let start = usize::from(self.at.start()); let end = usize::from(self.at.end()); (start, end) }; s.serialize_field("at", &at)?; s.serialize_field("message", &self.message)?; if let Some(suggestion) = &self.suggestion { s.serialize_field("suggestion", suggestion)?; } s.end() }}
#[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: 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 { #[must_use] pub fn with_replacement(at: TextRange, fix: impl Into<SyntaxElement>) -> Self { Self { at, fix: Replacement::SyntaxElement(fix.into()), } } #[must_use] pub fn with_empty(at: TextRange) -> Self { Self { at, fix: Replacement::Empty, } } /// Apply a suggestion to a source file pub fn apply(&self, src: &mut String) { let start = usize::from(self.at.start()); let end = usize::from(self.at.end()); src.replace_range(start..end, &self.fix.to_string()); }}
unsafe impl Send for Suggestion {}
#[cfg(feature = "json-out")]impl Serialize for Suggestion { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { let mut s = serializer.serialize_struct("Suggestion", 2)?; let at = { let start = usize::from(self.at.start()); let end = usize::from(self.at.end()); (start, end) }; let fix = self.fix.to_string(); s.serialize_field("at", &at)?; s.serialize_field("fix", &fix)?; s.end() }}
/// Lint logic is defined via this trait. Do not implement manually,/// look at the `lint` attribute macro instead for implementing rulespub trait Rule { fn validate(&self, node: &SyntaxElement) -> Option<Report>;}
/// Contains information about the lint itself. Do not implement manually,/// look at the `lint` attribute macro instead for implementing rulespub trait Metadata { 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<SyntaxKind>;}
/// Contains offline explanation for each lint/// The `lint` macro scans nearby doc comments for/// explanations and derives this trait.////// FIXME: the lint macro does way too much, maybe/// split it into smaller macros.pub trait Explain { fn explanation(&self) -> &'static str { "no explanation found" }}
/// Combines Rule and Metadata, do not implement manually, this is derived by/// the `lint` macro.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.////// See `lints.rs` for usage.#[macro_export]macro_rules! lints { ($($s:ident),*,) => { lints!($($s),*); }; ($($s:ident),*) => { $( mod $s; )* ::lazy_static::lazy_static! { pub static ref LINTS: Vec<&'static Box<dyn $crate::Lint>> = { let mut v = Vec::new(); $( { let temp_lint = &*$s::LINT; v.push(temp_lint); } )* v }; } }}