Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-diagnostics/src/handlers/cannot_be_dereferenced.rs')
-rw-r--r--crates/ide-diagnostics/src/handlers/cannot_be_dereferenced.rs74
1 files changed, 74 insertions, 0 deletions
diff --git a/crates/ide-diagnostics/src/handlers/cannot_be_dereferenced.rs b/crates/ide-diagnostics/src/handlers/cannot_be_dereferenced.rs
new file mode 100644
index 0000000000..6c9726f8af
--- /dev/null
+++ b/crates/ide-diagnostics/src/handlers/cannot_be_dereferenced.rs
@@ -0,0 +1,74 @@
+use hir::HirDisplay;
+
+use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
+
+// Diagnostic: cannot-be-dereferenced
+//
+// This diagnostic is triggered if the operand of a dereference expression
+// cannot be dereferenced.
+pub(crate) fn cannot_be_dereferenced(
+ ctx: &DiagnosticsContext<'_, '_>,
+ d: &hir::CannotBeDereferenced<'_>,
+) -> Diagnostic {
+ Diagnostic::new_with_syntax_node_ptr(
+ ctx,
+ DiagnosticCode::RustcHardError("E0614"),
+ format!(
+ "type `{}` cannot be dereferenced",
+ d.found.display(ctx.sema.db, ctx.display_target)
+ ),
+ d.expr.map(Into::into),
+ )
+ .stable()
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::tests::check_diagnostics;
+
+ #[test]
+ fn cannot_be_dereferenced() {
+ check_diagnostics(
+ r#"
+fn f() {
+ let x = 1i32;
+ let _ = *x;
+ //^^ error: type `i32` cannot be dereferenced
+}
+"#,
+ );
+ }
+
+ #[test]
+ fn allows_reference_deref() {
+ check_diagnostics(
+ r#"
+fn f(x: &i32) {
+ let _ = *x;
+}
+"#,
+ );
+ }
+
+ #[test]
+ fn allows_overloaded_deref() {
+ check_diagnostics(
+ r#"
+//- minicore: deref
+struct Wrapper(i32);
+
+impl core::ops::Deref for Wrapper {
+ type Target = i32;
+
+ fn deref(&self) -> &i32 {
+ &self.0
+ }
+}
+
+fn f(x: Wrapper) {
+ let _ = *x;
+}
+"#,
+ );
+ }
+}