From 36342b4b29dedde44b9aee6362ad5324e6282221 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 25 Apr 2022 15:21:30 +0200 Subject: [PATCH] Don't emit a quickfix for placeholder suggestions --- .../test_data/clippy_pass_by_ref.txt | 66 +------------------ .../rust-analyzer/src/diagnostics/to_proto.rs | 14 +++- 2 files changed, 13 insertions(+), 67 deletions(-) diff --git a/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt b/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt index 41c509452a9..c3b540e31f7 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt @@ -296,70 +296,6 @@ tags: None, data: None, }, - fix: Some( - Fix { - ranges: [ - Range { - start: Position { - line: 41, - character: 23, - }, - end: Position { - line: 41, - character: 28, - }, - }, - ], - action: CodeAction { - title: "consider passing by value instead: `self`", - group: None, - kind: Some( - CodeActionKind( - "quickfix", - ), - ), - command: None, - edit: Some( - SnippetWorkspaceEdit { - changes: Some( - { - Url { - scheme: "file", - cannot_be_a_base: false, - username: "", - password: None, - host: None, - port: None, - path: "/test/compiler/mir/tagset.rs", - query: None, - fragment: None, - }: [ - TextEdit { - range: Range { - start: Position { - line: 41, - character: 23, - }, - end: Position { - line: 41, - character: 28, - }, - }, - new_text: "self", - }, - ], - }, - ), - document_changes: None, - change_annotations: None, - }, - ), - is_preferred: Some( - true, - ), - data: None, - }, - }, - ), + fix: None, }, ] diff --git a/crates/rust-analyzer/src/diagnostics/to_proto.rs b/crates/rust-analyzer/src/diagnostics/to_proto.rs index e9a192cdfb0..4b780556566 100644 --- a/crates/rust-analyzer/src/diagnostics/to_proto.rs +++ b/crates/rust-analyzer/src/diagnostics/to_proto.rs @@ -2,7 +2,7 @@ //! `cargo check` json format to the LSP diagnostic format. use std::collections::HashMap; -use flycheck::{DiagnosticLevel, DiagnosticSpan}; +use flycheck::{Applicability, DiagnosticLevel, DiagnosticSpan}; use itertools::Itertools; use stdx::format_to; use vfs::{AbsPath, AbsPathBuf}; @@ -159,7 +159,17 @@ fn map_rust_child_diagnostic( } let location = location(config, workspace_root, span); let edit = lsp_types::TextEdit::new(location.range, suggested_replacement.clone()); - edit_map.entry(location.uri).or_default().push(edit); + + // Only actually emit a quickfix if the suggestion is "valid enough". + // We accept both "MaybeIncorrect" and "MachineApplicable". "MaybeIncorrect" means that + // the suggestion is *complete* (contains no placeholders where code needs to be + // inserted), but might not be what the user wants, or might need minor adjustments. + if matches!( + span.suggestion_applicability, + None | Some(Applicability::MaybeIncorrect | Applicability::MachineApplicable) + ) { + edit_map.entry(location.uri).or_default().push(edit); + } } }