feat(xatu): add manual inherit from lint #87

Merged
alexion merged 1 commits from xatu-manual-inherit-from into main 2026-09-19 23:31:04 -04:00
9 changed files with 105 additions and 11 deletions

View File

@@ -9,6 +9,7 @@ rust_library(
"parser.rs", "parser.rs",
"rules.rs", "rules.rs",
"rules/manual_inherit.rs", "rules/manual_inherit.rs",
"rules/manual_inherit_from.rs",
"rules/unquoted_uri.rs", "rules/unquoted_uri.rs",
"xatu.rs", "xatu.rs",
], ],

View File

@@ -34,7 +34,7 @@ pub enum NodeData {
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Binding { pub struct Binding {
pub key: Ident, pub key: Ident,
pub value: Ident, pub value: Expr,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@@ -42,6 +42,18 @@ pub struct Ident {
pub name: String, pub name: String,
} }
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Expr {
Ident(Ident),
Select(Select),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Select {
pub receiver: String,
pub attr: Ident,
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Token { pub struct Token {
pub range: SourceRange, pub range: SourceRange,

View File

@@ -67,15 +67,24 @@ impl TryFrom<rnix::ast::AttrpathValue> for ast::Binding {
return Err(()); return Err(());
} }
let value = match binding.value().ok_or(())? { let value = binding.value().ok_or(())?.try_into()?;
rnix::ast::Expr::Ident(ident) => ident.into(),
_ => return Err(()),
};
Ok(Self { key, value }) Ok(Self { key, value })
} }
} }
impl TryFrom<rnix::ast::Expr> for ast::Expr {
type Error = ();
fn try_from(expr: rnix::ast::Expr) -> Result<Self, Self::Error> {
match expr {
rnix::ast::Expr::Ident(ident) => Ok(Self::Ident(ident.into())),
rnix::ast::Expr::Select(select) => Ok(Self::Select(select.try_into()?)),
_ => Err(()),
}
}
}
impl From<rnix::ast::Ident> for ast::Ident { impl From<rnix::ast::Ident> for ast::Ident {
fn from(ident: rnix::ast::Ident) -> Self { fn from(ident: rnix::ast::Ident) -> Self {
Self { Self {
@@ -84,6 +93,31 @@ impl From<rnix::ast::Ident> for ast::Ident {
} }
} }
impl TryFrom<rnix::ast::Select> for ast::Select {
type Error = ();
fn try_from(select: rnix::ast::Select) -> Result<Self, Self::Error> {
if select.or_token().is_some() {
return Err(());
}
let mut attrs = select.attrpath().ok_or(())?.attrs();
let attr = match attrs.next().ok_or(())? {
Attr::Ident(ident) => ident.into(),
_ => return Err(()),
};
if attrs.next().is_some() {
return Err(());
}
Ok(Self {
receiver: select.expr().ok_or(())?.syntax().to_string(),
attr,
})
}
}
fn convert_token(token: rnix::SyntaxToken) -> Option<ast::Token> { fn convert_token(token: rnix::SyntaxToken) -> Option<ast::Token> {
match token.kind() { match token.kind() {
rnix::SyntaxKind::TOKEN_URI => Some(ast::Token { rnix::SyntaxKind::TOKEN_URI => Some(ast::Token {

View File

@@ -1,9 +1,11 @@
use crate::FileId; use crate::FileId;
use crate::ast; use crate::ast;
use crate::rules::manual_inherit::ManualInherit; use crate::rules::manual_inherit::ManualInherit;
use crate::rules::manual_inherit_from::ManualInheritFrom;
use crate::rules::unquoted_uri::UnquotedUri; use crate::rules::unquoted_uri::UnquotedUri;
pub mod manual_inherit; pub mod manual_inherit;
pub mod manual_inherit_from;
pub mod unquoted_uri; pub mod unquoted_uri;
#[derive(Debug)] #[derive(Debug)]
@@ -51,7 +53,12 @@ pub fn run_rules(file: &ast::ParsedFile) -> Vec<Diagnostic> {
let node_diagnostics = file let node_diagnostics = file
.nodes .nodes
.iter() .iter()
.flat_map(|node| [run_node_rule::<ManualInherit>(node)]) .flat_map(|node| {
[
run_node_rule::<ManualInherit>(node),
run_node_rule::<ManualInheritFrom>(node),
]
})
.flatten(); .flatten();
let token_diagnostics = file let token_diagnostics = file
.tokens .tokens

View File

@@ -14,10 +14,11 @@ impl NodeRule for ManualInherit {
} }
fn check(node: &Self::Node) -> Option<&'static str> { fn check(node: &Self::Node) -> Option<&'static str> {
if node.key == node.value { match &node.value {
Some("Use `inherit` for this assignment") ast::Expr::Ident(value) if node.key == *value => {
} else { Some("Use `inherit` for this assignment")
None }
_ => None,
} }
} }
} }

View File

@@ -0,0 +1,24 @@
use crate::{ast, rules::NodeRule};
pub struct ManualInheritFrom;
impl NodeRule for ManualInheritFrom {
type Node = ast::Binding;
const ID: &'static str = "manual_inherit_from";
fn select(data: &ast::NodeData) -> Option<&Self::Node> {
let ast::NodeData::Binding(binding) = data;
Some(binding)
}
fn check(node: &Self::Node) -> Option<&'static str> {
match &node.value {
ast::Expr::Select(select) if node.key == select.attr => {
Some("Use `inherit` for this selected attribute")
}
_ => None,
}
}
}

View File

@@ -8,5 +8,6 @@
nested.package = nested.package; nested.package = nested.package;
"quoted" = quoted; "quoted" = quoted;
${dynamic} = dynamic; ${dynamic} = dynamic;
from = pkgs.from; from = pkgs.from.other;
fallback = pkgs.fallback or null;
} }

View File

@@ -1,4 +1,5 @@
{ {
package = package; package = package;
from = pkgs.from;
url = https://example.com; url = https://example.com;
} }

View File

@@ -77,3 +77,16 @@ fn xatu_reports_manual_inherit() -> googletest::Result<()> {
expect_that!(manual_inherit_count, eq(1)); expect_that!(manual_inherit_count, eq(1));
Ok(()) Ok(())
} }
#[gtest]
fn xatu_reports_manual_inherit_from() -> googletest::Result<()> {
let report = xatu::future_sight(vec![fixture_source("testdata/lint_findings.nix")?]).or_fail()?;
let manual_inherit_from_count = report
.diagnostics
.iter()
.filter(|diagnostic| diagnostic.finding.rule_id == "manual_inherit_from")
.count();
expect_that!(manual_inherit_from_count, eq(1));
Ok(())
}