aboutsummaryrefslogtreecommitdiff
path: root/crates/hir
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-06-13 18:19:11 +0100
committerAleksey Kladov <[email protected]>2021-06-13 18:19:11 +0100
commit949a6ec469507db5e79578da94e17cb63cb54d19 (patch)
tree5e4ec22d50715d9855e751ed3f296e90e5bfaa73 /crates/hir
parent74f3cca85ab870614f314c6180e2fbb883ad4fe3 (diff)
internal: refactor missing or or some diagnostic
Diffstat (limited to 'crates/hir')
-rw-r--r--crates/hir/src/diagnostics.rs31
-rw-r--r--crates/hir/src/lib.rs6
2 files changed, 3 insertions, 34 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs
index b144bb335..9afee0b90 100644
--- a/crates/hir/src/diagnostics.rs
+++ b/crates/hir/src/diagnostics.rs
@@ -37,6 +37,7 @@ diagnostics![
37 MacroError, 37 MacroError,
38 MismatchedArgCount, 38 MismatchedArgCount,
39 MissingFields, 39 MissingFields,
40 MissingOkOrSomeInTailExpr,
40 MissingUnsafe, 41 MissingUnsafe,
41 NoSuchField, 42 NoSuchField,
42 RemoveThisSemicolon, 43 RemoveThisSemicolon,
@@ -157,41 +158,13 @@ pub struct RemoveThisSemicolon {
157 pub expr: InFile<AstPtr<ast::Expr>>, 158 pub expr: InFile<AstPtr<ast::Expr>>,
158} 159}
159 160
160// Diagnostic: missing-ok-or-some-in-tail-expr
161//
162// This diagnostic is triggered if a block that should return `Result` returns a value not wrapped in `Ok`,
163// or if a block that should return `Option` returns a value not wrapped in `Some`.
164//
165// Example:
166//
167// ```rust
168// fn foo() -> Result<u8, ()> {
169// 10
170// }
171// ```
172#[derive(Debug)] 161#[derive(Debug)]
173pub struct MissingOkOrSomeInTailExpr { 162pub struct MissingOkOrSomeInTailExpr {
174 pub file: HirFileId, 163 pub expr: InFile<AstPtr<ast::Expr>>,
175 pub expr: AstPtr<ast::Expr>,
176 // `Some` or `Ok` depending on whether the return type is Result or Option 164 // `Some` or `Ok` depending on whether the return type is Result or Option
177 pub required: String, 165 pub required: String,
178} 166}
179 167
180impl Diagnostic for MissingOkOrSomeInTailExpr {
181 fn code(&self) -> DiagnosticCode {
182 DiagnosticCode("missing-ok-or-some-in-tail-expr")
183 }
184 fn message(&self) -> String {
185 format!("wrap return expression in {}", self.required)
186 }
187 fn display_source(&self) -> InFile<SyntaxNodePtr> {
188 InFile { file_id: self.file, value: self.expr.clone().into() }
189 }
190 fn as_any(&self) -> &(dyn Any + Send + 'static) {
191 self
192 }
193}
194
195// Diagnostic: missing-match-arm 168// Diagnostic: missing-match-arm
196// 169//
197// This diagnostic is triggered if `match` block is missing one or more match arms. 170// 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 cb9bf60b8..aaab5336a 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -1190,11 +1190,7 @@ impl Function {
1190 } 1190 }
1191 BodyValidationDiagnostic::MissingOkOrSomeInTailExpr { expr, required } => { 1191 BodyValidationDiagnostic::MissingOkOrSomeInTailExpr { expr, required } => {
1192 match source_map.expr_syntax(expr) { 1192 match source_map.expr_syntax(expr) {
1193 Ok(source_ptr) => sink.push(MissingOkOrSomeInTailExpr { 1193 Ok(expr) => acc.push(MissingOkOrSomeInTailExpr { expr, required }.into()),
1194 file: source_ptr.file_id,
1195 expr: source_ptr.value,
1196 required,
1197 }),
1198 Err(SyntheticSyntax) => (), 1194 Err(SyntheticSyntax) => (),
1199 } 1195 }
1200 } 1196 }