1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
use crate::{make, session::SessionInfo, Metadata, Report, Rule, Suggestion};
use if_chain::if_chain;
use macros::lint;
use rnix::{
types::{BinOp, BinOpKind, IfElse, Select, TypedNode},
NodeOrToken, SyntaxElement, SyntaxKind,
};
/// ## What it does
/// Checks for expressions that use the "has attribute" operator: `?`,
/// where the `or` operator would suffice.
///
/// ## Why is this bad?
/// The `or` operator is more readable.
///
/// ## Example
/// ```nix
/// if x ? a then x.a else some_default
/// ```
///
/// Use `or` instead:
///
/// ```nix
/// x.a or some_default
/// ```
#[lint(
name = "useless_has_attr",
note = "This `if` expression can be simplified with `or`",
code = 19,
match_with = SyntaxKind::NODE_IF_ELSE
)]
struct UselessHasAttr;
impl Rule for UselessHasAttr {
fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option<Report> {
if_chain! {
if let NodeOrToken::Node(node) = node;
if let Some(if_else_expr) = IfElse::cast(node.clone());
if let Some(condition_expr) = if_else_expr.condition();
if let Some(default_expr) = if_else_expr.else_body();
if let Some(cond_bin_expr) = BinOp::cast(condition_expr.clone());
if let Some(BinOpKind::IsSet) = cond_bin_expr.operator();
// set ? attr_path
// ^^^--------------- lhs
// ^^^^^^^^^^--- rhs
if let Some(set) = cond_bin_expr.lhs();
if let Some(attr_path) = cond_bin_expr.rhs();
// check if body of the `if` expression is of the form `set.attr_path`
if let Some(body_expr) = if_else_expr.body();
if let Some(body_select_expr) = Select::cast(body_expr.clone());
let expected_body = make::select(&set, &attr_path);
// text comparison will do for now
if body_select_expr.node().text() == expected_body.node().text();
then {
let at = node.text_range();
// `or` is tightly binding, we need to parenthesize non-literal exprs
let default_with_parens = match default_expr.kind() {
SyntaxKind::NODE_LIST
| SyntaxKind::NODE_PAREN
| SyntaxKind::NODE_STRING
| SyntaxKind::NODE_ATTR_SET
| SyntaxKind::NODE_IDENT => default_expr,
_ => make::parenthesize(&default_expr).node().clone(),
};
let replacement = make::or_default(&set, &attr_path, &default_with_parens).node().clone();
let message = format!(
"Consider using `{}` instead of this `if` expression",
replacement
);
Some(
self.report()
.suggest(at, message, Suggestion::new(at, replacement)),
)
} else {
None
}
}
}
}
|