From 7457498ea64c4542b501d4c03ec2ff617763b070 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 30 Jan 2022 12:30:51 +0530 Subject: new lint: deprecated_to_path --- lib/src/lints/deprecated_to_path.rs | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 lib/src/lints/deprecated_to_path.rs (limited to 'lib/src/lints') diff --git a/lib/src/lints/deprecated_to_path.rs b/lib/src/lints/deprecated_to_path.rs new file mode 100644 index 0000000..36cbe35 --- /dev/null +++ b/lib/src/lints/deprecated_to_path.rs @@ -0,0 +1,59 @@ +use crate::{session::SessionInfo, Metadata, Report, Rule}; + +use if_chain::if_chain; +use macros::lint; +use rnix::{ + types::{Apply, TypedNode}, + NodeOrToken, SyntaxElement, SyntaxKind, +}; + +/// ## What it does +/// Checks for usage of the `toPath` function. +/// +/// ## Why is this bad? +/// `toPath` is deprecated. +/// +/// ## Example +/// +/// ```nix +/// builtins.toPath "/path" +/// ``` +/// +/// Try these instead: +/// +/// ```nix +/// # to convert the string to an absolute path: +/// /. + "/path" +/// # => /abc +/// +/// # to convert the string to a path relative to the current directory: +/// ./. + "/bin" +/// # => /home/np/statix/bin +/// ``` +#[lint( + name = "deprecated_to_path", + note = "Found usage of deprecated builtin toPath", + code = 17, + match_with = SyntaxKind::NODE_APPLY +)] +struct DeprecatedIsNull; + +static ALLOWED_PATHS: &[&str; 2] = &["builtins.toPath", "toPath"]; + +impl Rule for DeprecatedIsNull { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { + if_chain! { + if let NodeOrToken::Node(node) = node; + if let Some(apply) = Apply::cast(node.clone()); + let lambda_path = apply.lambda()?.to_string(); + if ALLOWED_PATHS.iter().any(|&p| p == lambda_path.as_str()); + then { + let at = node.text_range(); + let message = format!("`{}` is deprecated, see `:doc builtins.toPath` within the REPL for more", lambda_path); + Some(self.report().diagnostic(at, message)) + } else { + None + } + } + } +} -- cgit v1.2.3