aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_diagnostics/src/handlers/useless_braces.rs
blob: 8b9330e040137a183977f3ef2ddcf1dd95d55249 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use ide_db::{base_db::FileId, source_change::SourceChange};
use itertools::Itertools;
use syntax::{ast, AstNode, SyntaxNode, TextRange};
use text_edit::TextEdit;

use crate::{fix, Diagnostic, Severity};

// Diagnostic: unnecessary-braces
//
// Diagnostic for unnecessary braces in `use` items.
pub(crate) fn useless_braces(
    acc: &mut Vec<Diagnostic>,
    file_id: FileId,
    node: &SyntaxNode,
) -> Option<()> {
    let use_tree_list = ast::UseTreeList::cast(node.clone())?;
    if let Some((single_use_tree,)) = use_tree_list.use_trees().collect_tuple() {
        // If there is a comment inside the bracketed `use`,
        // assume it is a commented out module path and don't show diagnostic.
        if use_tree_list.has_inner_comment() {
            return Some(());
        }

        let use_range = use_tree_list.syntax().text_range();
        let edit = remove_braces(&single_use_tree).unwrap_or_else(|| {
            let to_replace = single_use_tree.syntax().text().to_string();
            let mut edit_builder = TextEdit::builder();
            edit_builder.delete(use_range);
            edit_builder.insert(use_range.start(), to_replace);
            edit_builder.finish()
        });

        acc.push(
            Diagnostic::new(
                "unnecessary-braces",
                "Unnecessary braces in use statement".to_string(),
                use_range,
            )
            .severity(Severity::WeakWarning)
            .with_fixes(Some(vec![fix(
                "remove_braces",
                "Remove unnecessary braces",
                SourceChange::from_text_edit(file_id, edit),
                use_range,
            )])),
        );
    }

    Some(())
}

fn remove_braces(single_use_tree: &ast::UseTree) -> Option<TextEdit> {
    let use_tree_list_node = single_use_tree.syntax().parent()?;
    if single_use_tree.path()?.segment()?.self_token().is_some() {
        let start = use_tree_list_node.prev_sibling_or_token()?.text_range().start();
        let end = use_tree_list_node.text_range().end();
        return Some(TextEdit::delete(TextRange::new(start, end)));
    }
    None
}

#[cfg(test)]
mod tests {
    use crate::tests::{check_diagnostics, check_fix};

    #[test]
    fn test_check_unnecessary_braces_in_use_statement() {
        check_diagnostics(
            r#"
use a;
use a::{c, d::e};

mod a {
    mod c {}
    mod d {
        mod e {}
    }
}
"#,
        );
        check_diagnostics(
            r#"
use a;
use a::{
    c,
    // d::e
};

mod a {
    mod c {}
    mod d {
        mod e {}
    }
}
"#,
        );
        check_fix(
            r#"
mod b {}
use {$0b};
"#,
            r#"
mod b {}
use b;
"#,
        );
        check_fix(
            r#"
mod b {}
use {b$0};
"#,
            r#"
mod b {}
use b;
"#,
        );
        check_fix(
            r#"
mod a { mod c {} }
use a::{c$0};
"#,
            r#"
mod a { mod c {} }
use a::c;
"#,
        );
        check_fix(
            r#"
mod a {}
use a::{self$0};
"#,
            r#"
mod a {}
use a;
"#,
        );
        check_fix(
            r#"
mod a { mod c {} mod d { mod e {} } }
use a::{c, d::{e$0}};
"#,
            r#"
mod a { mod c {} mod d { mod e {} } }
use a::{c, d::e};
"#,
        );
    }
}