aboutsummaryrefslogtreecommitdiff
path: root/crates/hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir/src')
-rw-r--r--crates/hir/src/diagnostics.rs72
-rw-r--r--crates/hir/src/lib.rs22
2 files changed, 14 insertions, 80 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs
index f839616ce..c294a803b 100644
--- a/crates/hir/src/diagnostics.rs
+++ b/crates/hir/src/diagnostics.rs
@@ -37,8 +37,11 @@ diagnostics![
37 MacroError, 37 MacroError,
38 MismatchedArgCount, 38 MismatchedArgCount,
39 MissingFields, 39 MissingFields,
40 MissingOkOrSomeInTailExpr,
40 MissingUnsafe, 41 MissingUnsafe,
41 NoSuchField, 42 NoSuchField,
43 RemoveThisSemicolon,
44 ReplaceFilterMapNextWithFindMap,
42 UnimplementedBuiltinMacro, 45 UnimplementedBuiltinMacro,
43 UnresolvedExternCrate, 46 UnresolvedExternCrate,
44 UnresolvedImport, 47 UnresolvedImport,
@@ -119,9 +122,6 @@ pub struct MissingFields {
119 pub missed_fields: Vec<Name>, 122 pub missed_fields: Vec<Name>,
120} 123}
121 124
122// Diagnostic: replace-filter-map-next-with-find-map
123//
124// This diagnostic is triggered when `.filter_map(..).next()` is used, rather than the more concise `.find_map(..)`.
125#[derive(Debug)] 125#[derive(Debug)]
126pub struct ReplaceFilterMapNextWithFindMap { 126pub struct ReplaceFilterMapNextWithFindMap {
127 pub file: HirFileId, 127 pub file: HirFileId,
@@ -129,21 +129,6 @@ pub struct ReplaceFilterMapNextWithFindMap {
129 pub next_expr: AstPtr<ast::Expr>, 129 pub next_expr: AstPtr<ast::Expr>,
130} 130}
131 131
132impl Diagnostic for ReplaceFilterMapNextWithFindMap {
133 fn code(&self) -> DiagnosticCode {
134 DiagnosticCode("replace-filter-map-next-with-find-map")
135 }
136 fn message(&self) -> String {
137 "replace filter_map(..).next() with find_map(..)".to_string()
138 }
139 fn display_source(&self) -> InFile<SyntaxNodePtr> {
140 InFile { file_id: self.file, value: self.next_expr.clone().into() }
141 }
142 fn as_any(&self) -> &(dyn Any + Send + 'static) {
143 self
144 }
145}
146
147#[derive(Debug)] 132#[derive(Debug)]
148pub struct MismatchedArgCount { 133pub struct MismatchedArgCount {
149 pub call_expr: InFile<AstPtr<ast::Expr>>, 134 pub call_expr: InFile<AstPtr<ast::Expr>>,
@@ -153,63 +138,16 @@ pub struct MismatchedArgCount {
153 138
154#[derive(Debug)] 139#[derive(Debug)]
155pub struct RemoveThisSemicolon { 140pub struct RemoveThisSemicolon {
156 pub file: HirFileId, 141 pub expr: InFile<AstPtr<ast::Expr>>,
157 pub expr: AstPtr<ast::Expr>,
158}
159
160impl Diagnostic for RemoveThisSemicolon {
161 fn code(&self) -> DiagnosticCode {
162 DiagnosticCode("remove-this-semicolon")
163 }
164
165 fn message(&self) -> String {
166 "Remove this semicolon".to_string()
167 }
168
169 fn display_source(&self) -> InFile<SyntaxNodePtr> {
170 InFile { file_id: self.file, value: self.expr.clone().into() }
171 }
172
173 fn as_any(&self) -> &(dyn Any + Send + 'static) {
174 self
175 }
176} 142}
177 143
178// Diagnostic: missing-ok-or-some-in-tail-expr
179//
180// This diagnostic is triggered if a block that should return `Result` returns a value not wrapped in `Ok`,
181// or if a block that should return `Option` returns a value not wrapped in `Some`.
182//
183// Example:
184//
185// ```rust
186// fn foo() -> Result<u8, ()> {
187// 10
188// }
189// ```
190#[derive(Debug)] 144#[derive(Debug)]
191pub struct MissingOkOrSomeInTailExpr { 145pub struct MissingOkOrSomeInTailExpr {
192 pub file: HirFileId, 146 pub expr: InFile<AstPtr<ast::Expr>>,
193 pub expr: AstPtr<ast::Expr>,
194 // `Some` or `Ok` depending on whether the return type is Result or Option 147 // `Some` or `Ok` depending on whether the return type is Result or Option
195 pub required: String, 148 pub required: String,
196} 149}
197 150
198impl Diagnostic for MissingOkOrSomeInTailExpr {
199 fn code(&self) -> DiagnosticCode {
200 DiagnosticCode("missing-ok-or-some-in-tail-expr")
201 }
202 fn message(&self) -> String {
203 format!("wrap return expression in {}", self.required)
204 }
205 fn display_source(&self) -> InFile<SyntaxNodePtr> {
206 InFile { file_id: self.file, value: self.expr.clone().into() }
207 }
208 fn as_any(&self) -> &(dyn Any + Send + 'static) {
209 self
210 }
211}
212
213// Diagnostic: missing-match-arm 151// Diagnostic: missing-match-arm
214// 152//
215// This diagnostic is triggered if `match` block is missing one or more match arms. 153// This diagnostic is triggered if `match` block is missing one or more match arms.
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index c1af5f097..b2731b62f 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -1168,10 +1168,13 @@ impl Function {
1168 } 1168 }
1169 BodyValidationDiagnostic::ReplaceFilterMapNextWithFindMap { method_call_expr } => { 1169 BodyValidationDiagnostic::ReplaceFilterMapNextWithFindMap { method_call_expr } => {
1170 if let Ok(next_source_ptr) = source_map.expr_syntax(method_call_expr) { 1170 if let Ok(next_source_ptr) = source_map.expr_syntax(method_call_expr) {
1171 sink.push(ReplaceFilterMapNextWithFindMap { 1171 acc.push(
1172 file: next_source_ptr.file_id, 1172 ReplaceFilterMapNextWithFindMap {
1173 next_expr: next_source_ptr.value, 1173 file: next_source_ptr.file_id,
1174 }); 1174 next_expr: next_source_ptr.value,
1175 }
1176 .into(),
1177 );
1175 } 1178 }
1176 } 1179 }
1177 BodyValidationDiagnostic::MismatchedArgCount { call_expr, expected, found } => { 1180 BodyValidationDiagnostic::MismatchedArgCount { call_expr, expected, found } => {
@@ -1184,20 +1187,13 @@ impl Function {
1184 } 1187 }
1185 BodyValidationDiagnostic::RemoveThisSemicolon { expr } => { 1188 BodyValidationDiagnostic::RemoveThisSemicolon { expr } => {
1186 match source_map.expr_syntax(expr) { 1189 match source_map.expr_syntax(expr) {
1187 Ok(source_ptr) => sink.push(RemoveThisSemicolon { 1190 Ok(expr) => acc.push(RemoveThisSemicolon { expr }.into()),
1188 file: source_ptr.file_id,
1189 expr: source_ptr.value,
1190 }),
1191 Err(SyntheticSyntax) => (), 1191 Err(SyntheticSyntax) => (),
1192 } 1192 }
1193 } 1193 }
1194 BodyValidationDiagnostic::MissingOkOrSomeInTailExpr { expr, required } => { 1194 BodyValidationDiagnostic::MissingOkOrSomeInTailExpr { expr, required } => {
1195 match source_map.expr_syntax(expr) { 1195 match source_map.expr_syntax(expr) {
1196 Ok(source_ptr) => sink.push(MissingOkOrSomeInTailExpr { 1196 Ok(expr) => acc.push(MissingOkOrSomeInTailExpr { expr, required }.into()),
1197 file: source_ptr.file_id,
1198 expr: source_ptr.value,
1199 required,
1200 }),
1201 Err(SyntheticSyntax) => (), 1197 Err(SyntheticSyntax) => (),
1202 } 1198 }
1203 } 1199 }