aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lints/bool_simplification.rs
blob: 9ccb1f60bf3c6c7b7db520535226238299943df0 (plain)
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
use crate::{make, session::SessionInfo, Metadata, Report, Rule, Suggestion};

use if_chain::if_chain;
use macros::lint;
use rnix::{
    types::{BinOp, BinOpKind, Paren, TypedNode, UnaryOp, UnaryOpKind, Wrapper},
    NodeOrToken, SyntaxElement, SyntaxKind,
};

/// ## What it does
/// Checks for boolean expressions that can be simplified.
///
/// ## Why is this bad?
/// Complex booleans affect readibility.
///
/// ## Example
/// ```nix
/// if !(x == y) then 0 else 1
/// ```
///
/// Use `!=` instead:
///
/// ```nix
/// if x != y then 0 else 1
/// ```
#[lint(
    name = "bool_simplification",
    note = "This boolean expression can be simplified",
    code = 18,
    match_with = SyntaxKind::NODE_UNARY_OP
)]
struct BoolSimplification;

impl Rule for BoolSimplification {
    fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option<Report> {
        if_chain! {
            if let NodeOrToken::Node(node) = node;
            if let Some(unary_expr) = UnaryOp::cast(node.clone());
            if unary_expr.operator() == UnaryOpKind::Invert;
            if let Some(value_expr) = unary_expr.value();
            if let Some(paren_expr) = Paren::cast(value_expr.clone());
            if let Some(inner_expr) = paren_expr.inner();
            if let Some(bin_expr) = BinOp::cast(inner_expr);
            if let Some(BinOpKind::Equal) = bin_expr.operator();
            then {
                let at = node.text_range();
                let message = "Try `!=` instead of `!(... == ...)`";

                let lhs = bin_expr.lhs()?;
                let rhs = bin_expr.rhs()?;
                let replacement = make::binary(&lhs, "!=", &rhs).node().clone();
                Some(
                    self.report()
                        .suggest(at, message, Suggestion::new(at, replacement)),
                )
            } else {
                None
            }
        }
    }
}