aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/diagnostics/field_shorthand.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/diagnostics/field_shorthand.rs')
-rw-r--r--crates/ide/src/diagnostics/field_shorthand.rs33
1 files changed, 18 insertions, 15 deletions
diff --git a/crates/ide/src/diagnostics/field_shorthand.rs b/crates/ide/src/diagnostics/field_shorthand.rs
index 01bd2dba6..c7f4dab8e 100644
--- a/crates/ide/src/diagnostics/field_shorthand.rs
+++ b/crates/ide/src/diagnostics/field_shorthand.rs
@@ -5,7 +5,7 @@ use ide_db::{base_db::FileId, source_change::SourceChange};
5use syntax::{ast, match_ast, AstNode, SyntaxNode}; 5use syntax::{ast, match_ast, AstNode, SyntaxNode};
6use text_edit::TextEdit; 6use text_edit::TextEdit;
7 7
8use crate::{diagnostics::fix, Diagnostic}; 8use crate::{diagnostics::fix, Diagnostic, Severity};
9 9
10pub(super) fn check(acc: &mut Vec<Diagnostic>, file_id: FileId, node: &SyntaxNode) { 10pub(super) fn check(acc: &mut Vec<Diagnostic>, file_id: FileId, node: &SyntaxNode) {
11 match_ast! { 11 match_ast! {
@@ -46,7 +46,8 @@ fn check_expr_field_shorthand(
46 46
47 let field_range = record_field.syntax().text_range(); 47 let field_range = record_field.syntax().text_range();
48 acc.push( 48 acc.push(
49 Diagnostic::hint(field_range, "Shorthand struct initialization".to_string()) 49 Diagnostic::new("use-field-shorthand", "Shorthand struct initialization", field_range)
50 .severity(Severity::WeakWarning)
50 .with_fixes(Some(vec![fix( 51 .with_fixes(Some(vec![fix(
51 "use_expr_field_shorthand", 52 "use_expr_field_shorthand",
52 "Use struct shorthand initialization", 53 "Use struct shorthand initialization",
@@ -85,30 +86,32 @@ fn check_pat_field_shorthand(
85 let edit = edit_builder.finish(); 86 let edit = edit_builder.finish();
86 87
87 let field_range = record_pat_field.syntax().text_range(); 88 let field_range = record_pat_field.syntax().text_range();
88 acc.push(Diagnostic::hint(field_range, "Shorthand struct pattern".to_string()).with_fixes( 89 acc.push(
89 Some(vec![fix( 90 Diagnostic::new("use-field-shorthand", "Shorthand struct pattern", field_range)
90 "use_pat_field_shorthand", 91 .severity(Severity::WeakWarning)
91 "Use struct field shorthand", 92 .with_fixes(Some(vec![fix(
92 SourceChange::from_text_edit(file_id, edit), 93 "use_pat_field_shorthand",
93 field_range, 94 "Use struct field shorthand",
94 )]), 95 SourceChange::from_text_edit(file_id, edit),
95 )); 96 field_range,
97 )])),
98 );
96 } 99 }
97} 100}
98 101
99#[cfg(test)] 102#[cfg(test)]
100mod tests { 103mod tests {
101 use crate::diagnostics::tests::{check_fix, check_no_diagnostics}; 104 use crate::diagnostics::tests::{check_diagnostics, check_fix};
102 105
103 #[test] 106 #[test]
104 fn test_check_expr_field_shorthand() { 107 fn test_check_expr_field_shorthand() {
105 check_no_diagnostics( 108 check_diagnostics(
106 r#" 109 r#"
107struct A { a: &'static str } 110struct A { a: &'static str }
108fn main() { A { a: "hello" } } 111fn main() { A { a: "hello" } }
109"#, 112"#,
110 ); 113 );
111 check_no_diagnostics( 114 check_diagnostics(
112 r#" 115 r#"
113struct A(usize); 116struct A(usize);
114fn main() { A { 0: 0 } } 117fn main() { A { 0: 0 } }
@@ -154,13 +157,13 @@ fn main() {
154 157
155 #[test] 158 #[test]
156 fn test_check_pat_field_shorthand() { 159 fn test_check_pat_field_shorthand() {
157 check_no_diagnostics( 160 check_diagnostics(
158 r#" 161 r#"
159struct A { a: &'static str } 162struct A { a: &'static str }
160fn f(a: A) { let A { a: hello } = a; } 163fn f(a: A) { let A { a: hello } = a; }
161"#, 164"#,
162 ); 165 );
163 check_no_diagnostics( 166 check_diagnostics(
164 r#" 167 r#"
165struct A(usize); 168struct A(usize);
166fn f(a: A) { let A { 0: 0 } = a; } 169fn f(a: A) { let A { 0: 0 } = a; }