mirror of
https://github.com/oppiliappan/statix.git
synced 2024-10-26 11:48:40 +03:00
new lint: deprecated_to_path
This commit is contained in:
parent
adacdb07ca
commit
7457498ea6
6
bin/tests/data/deprecated_to_path.nix
Normal file
6
bin/tests/data/deprecated_to_path.nix
Normal file
@ -0,0 +1,6 @@
|
||||
[
|
||||
(builtins.toPath x)
|
||||
(toPath x)
|
||||
(toPath "/abc/def")
|
||||
(builtins.toPath "/some/path")
|
||||
]
|
@ -62,5 +62,6 @@ test_lint! {
|
||||
deprecated_is_null,
|
||||
empty_inherit,
|
||||
faster_groupby => session_info!("2.5"),
|
||||
faster_zipattrswith => session_info!("2.6")
|
||||
faster_zipattrswith => session_info!("2.6"),
|
||||
deprecated_to_path => session_info!("2.4")
|
||||
}
|
||||
|
34
bin/tests/snapshots/main__deprecated_to_path.snap
Normal file
34
bin/tests/snapshots/main__deprecated_to_path.snap
Normal file
@ -0,0 +1,34 @@
|
||||
---
|
||||
source: bin/tests/main.rs
|
||||
expression: "&out"
|
||||
|
||||
---
|
||||
[W17] Warning: Found usage of deprecated builtin toPath
|
||||
╭─[data/deprecated_to_path.nix:2:4]
|
||||
│
|
||||
2 │ (builtins.toPath x)
|
||||
· ────────┬────────
|
||||
· ╰────────── builtins.toPath is deprecated, see :doc builtins.toPath within the REPL for more
|
||||
───╯
|
||||
[W17] Warning: Found usage of deprecated builtin toPath
|
||||
╭─[data/deprecated_to_path.nix:3:4]
|
||||
│
|
||||
3 │ (toPath x)
|
||||
· ────┬───
|
||||
· ╰───── toPath is deprecated, see :doc builtins.toPath within the REPL for more
|
||||
───╯
|
||||
[W17] Warning: Found usage of deprecated builtin toPath
|
||||
╭─[data/deprecated_to_path.nix:4:4]
|
||||
│
|
||||
4 │ (toPath "/abc/def")
|
||||
· ────────┬────────
|
||||
· ╰────────── toPath is deprecated, see :doc builtins.toPath within the REPL for more
|
||||
───╯
|
||||
[W17] Warning: Found usage of deprecated builtin toPath
|
||||
╭─[data/deprecated_to_path.nix:5:4]
|
||||
│
|
||||
5 │ (builtins.toPath "/some/path")
|
||||
· ──────────────┬─────────────
|
||||
· ╰─────────────── builtins.toPath is deprecated, see :doc builtins.toPath within the REPL for more
|
||||
───╯
|
||||
|
@ -17,4 +17,5 @@ lints! {
|
||||
empty_inherit,
|
||||
faster_groupby,
|
||||
faster_zipattrswith,
|
||||
deprecated_to_path,
|
||||
}
|
||||
|
59
lib/src/lints/deprecated_to_path.rs
Normal file
59
lib/src/lints/deprecated_to_path.rs
Normal file
@ -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<Report> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user