]> git.lizzy.rs Git - rust.git/commitdiff
Don't duplicate rustc diagnostics fixes
authorJonas Schievink <jonasschievink@gmail.com>
Fri, 15 Jan 2021 17:11:54 +0000 (18:11 +0100)
committerJonas Schievink <jonasschievink@gmail.com>
Fri, 15 Jan 2021 17:11:54 +0000 (18:11 +0100)
crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt
crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt
crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt
crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt
crates/rust-analyzer/src/diagnostics/to_proto.rs

index 23d42b4d08b8a057c4ebe2fb60fb40b3acee29e3..5c282fe673c82c83b0ebd4115578ccd8c9a9d4da 100644 (file)
             ),
             data: None,
         },
-        fixes: [
-            CodeAction {
-                title: "consider prefixing with an underscore",
-                group: None,
-                kind: Some(
-                    CodeActionKind(
-                        "quickfix",
-                    ),
-                ),
-                edit: Some(
-                    SnippetWorkspaceEdit {
-                        changes: Some(
-                            {
-                                Url {
-                                    scheme: "file",
-                                    host: None,
-                                    port: None,
-                                    path: "/test/driver/subcommand/repl.rs",
-                                    query: None,
-                                    fragment: None,
-                                }: [
-                                    TextEdit {
-                                        range: Range {
-                                            start: Position {
-                                                line: 290,
-                                                character: 8,
-                                            },
-                                            end: Position {
-                                                line: 290,
-                                                character: 11,
-                                            },
-                                        },
-                                        new_text: "_foo",
-                                    },
-                                ],
-                            },
-                        ),
-                        document_changes: None,
-                    },
-                ),
-                is_preferred: Some(
-                    true,
-                ),
-                data: None,
-            },
-        ],
+        fixes: [],
     },
     MappedRustDiagnostic {
         url: Url {
index 4e428bedcbf3d46088f8c480302773d1d8cd32ec..d36d7693d7f500a43c6ad1bfc7c2e3941b7aa9ae 100644 (file)
             ),
             data: None,
         },
-        fixes: [
-            CodeAction {
-                title: "consider prefixing with an underscore",
-                group: None,
-                kind: Some(
-                    CodeActionKind(
-                        "quickfix",
-                    ),
-                ),
-                edit: Some(
-                    SnippetWorkspaceEdit {
-                        changes: Some(
-                            {
-                                Url {
-                                    scheme: "file",
-                                    host: None,
-                                    port: None,
-                                    path: "/test/driver/subcommand/repl.rs",
-                                    query: None,
-                                    fragment: None,
-                                }: [
-                                    TextEdit {
-                                        range: Range {
-                                            start: Position {
-                                                line: 290,
-                                                character: 8,
-                                            },
-                                            end: Position {
-                                                line: 290,
-                                                character: 11,
-                                            },
-                                        },
-                                        new_text: "_foo",
-                                    },
-                                ],
-                            },
-                        ),
-                        document_changes: None,
-                    },
-                ),
-                is_preferred: Some(
-                    true,
-                ),
-                data: None,
-            },
-        ],
+        fixes: [],
     },
     MappedRustDiagnostic {
         url: Url {
index 4ddd7efae366eb9f7e46f1bd72da8c2e23a71490..17845b711aa0375b0ea0eb284a34070a0539780e 100644 (file)
             ),
             data: None,
         },
-        fixes: [
-            CodeAction {
-                title: "consider prefixing with an underscore",
-                group: None,
-                kind: Some(
-                    CodeActionKind(
-                        "quickfix",
-                    ),
-                ),
-                edit: Some(
-                    SnippetWorkspaceEdit {
-                        changes: Some(
-                            {
-                                Url {
-                                    scheme: "file",
-                                    host: None,
-                                    port: None,
-                                    path: "/test/driver/subcommand/repl.rs",
-                                    query: None,
-                                    fragment: None,
-                                }: [
-                                    TextEdit {
-                                        range: Range {
-                                            start: Position {
-                                                line: 290,
-                                                character: 8,
-                                            },
-                                            end: Position {
-                                                line: 290,
-                                                character: 11,
-                                            },
-                                        },
-                                        new_text: "_foo",
-                                    },
-                                ],
-                            },
-                        ),
-                        document_changes: None,
-                    },
-                ),
-                is_preferred: Some(
-                    true,
-                ),
-                data: None,
-            },
-        ],
+        fixes: [],
     },
     MappedRustDiagnostic {
         url: Url {
index 4cbdb3b922f70c9c49fb001945dd1d089df50916..a199621678632a2d4c43233747bce211a27df9ea 100644 (file)
             tags: None,
             data: None,
         },
-        fixes: [
-            CodeAction {
-                title: "return the expression directly",
-                group: None,
-                kind: Some(
-                    CodeActionKind(
-                        "quickfix",
-                    ),
-                ),
-                edit: Some(
-                    SnippetWorkspaceEdit {
-                        changes: Some(
-                            {
-                                Url {
-                                    scheme: "file",
-                                    host: None,
-                                    port: None,
-                                    path: "/test/src/main.rs",
-                                    query: None,
-                                    fragment: None,
-                                }: [
-                                    TextEdit {
-                                        range: Range {
-                                            start: Position {
-                                                line: 2,
-                                                character: 4,
-                                            },
-                                            end: Position {
-                                                line: 2,
-                                                character: 30,
-                                            },
-                                        },
-                                        new_text: "",
-                                    },
-                                    TextEdit {
-                                        range: Range {
-                                            start: Position {
-                                                line: 3,
-                                                character: 4,
-                                            },
-                                            end: Position {
-                                                line: 3,
-                                                character: 5,
-                                            },
-                                        },
-                                        new_text: "(0..10).collect()",
-                                    },
-                                ],
-                            },
-                        ),
-                        document_changes: None,
-                    },
-                ),
-                is_preferred: Some(
-                    true,
-                ),
-                data: None,
-            },
-        ],
+        fixes: [],
     },
     MappedRustDiagnostic {
         url: Url {
             tags: None,
             data: None,
         },
-        fixes: [
-            CodeAction {
-                title: "return the expression directly",
-                group: None,
-                kind: Some(
-                    CodeActionKind(
-                        "quickfix",
-                    ),
-                ),
-                edit: Some(
-                    SnippetWorkspaceEdit {
-                        changes: Some(
-                            {
-                                Url {
-                                    scheme: "file",
-                                    host: None,
-                                    port: None,
-                                    path: "/test/src/main.rs",
-                                    query: None,
-                                    fragment: None,
-                                }: [
-                                    TextEdit {
-                                        range: Range {
-                                            start: Position {
-                                                line: 2,
-                                                character: 4,
-                                            },
-                                            end: Position {
-                                                line: 2,
-                                                character: 30,
-                                            },
-                                        },
-                                        new_text: "",
-                                    },
-                                    TextEdit {
-                                        range: Range {
-                                            start: Position {
-                                                line: 3,
-                                                character: 4,
-                                            },
-                                            end: Position {
-                                                line: 3,
-                                                character: 5,
-                                            },
-                                        },
-                                        new_text: "(0..10).collect()",
-                                    },
-                                ],
-                            },
-                        ),
-                        document_changes: None,
-                    },
-                ),
-                is_preferred: Some(
-                    true,
-                ),
-                data: None,
-            },
-        ],
+        fixes: [],
     },
     MappedRustDiagnostic {
         url: Url {
index 5407591987551426e722696dd66e93824ac09427..757899484f10633bcba7b6518949721420a1881f 100644 (file)
@@ -74,11 +74,13 @@ fn diagnostic_related_information(
     Some(lsp_types::DiagnosticRelatedInformation { location, message })
 }
 
+struct SubDiagnostic {
+    related: lsp_types::DiagnosticRelatedInformation,
+    suggested_fix: Option<lsp_ext::CodeAction>,
+}
+
 enum MappedRustChildDiagnostic {
-    Related {
-        related: lsp_types::DiagnosticRelatedInformation,
-        suggested_fix: Option<lsp_ext::CodeAction>,
-    },
+    SubDiagnostic(SubDiagnostic),
     MessageLine(String),
 }
 
@@ -105,15 +107,15 @@ fn map_rust_child_diagnostic(
     }
 
     if edit_map.is_empty() {
-        MappedRustChildDiagnostic::Related {
+        MappedRustChildDiagnostic::SubDiagnostic(SubDiagnostic {
             related: lsp_types::DiagnosticRelatedInformation {
                 location: location(workspace_root, spans[0]),
                 message: rd.message.clone(),
             },
             suggested_fix: None,
-        }
+        })
     } else {
-        MappedRustChildDiagnostic::Related {
+        MappedRustChildDiagnostic::SubDiagnostic(SubDiagnostic {
             related: lsp_types::DiagnosticRelatedInformation {
                 location: location(workspace_root, spans[0]),
                 message: rd.message.clone(),
@@ -130,7 +132,7 @@ fn map_rust_child_diagnostic(
                 is_preferred: Some(true),
                 data: None,
             }),
-        }
+        })
     }
 }
 
@@ -175,26 +177,22 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
     }
 
     let mut needs_primary_span_label = true;
-    let mut related_information = Vec::new();
+    let mut subdiagnostics = Vec::new();
     let mut tags = Vec::new();
 
     for secondary_span in rd.spans.iter().filter(|s| !s.is_primary) {
         let related = diagnostic_related_information(workspace_root, secondary_span);
         if let Some(related) = related {
-            related_information.push(related);
+            subdiagnostics.push(SubDiagnostic { related, suggested_fix: None });
         }
     }
 
-    let mut fixes = Vec::new();
     let mut message = rd.message.clone();
     for child in &rd.children {
         let child = map_rust_child_diagnostic(workspace_root, &child);
         match child {
-            MappedRustChildDiagnostic::Related { related, suggested_fix } => {
-                related_information.push(related);
-                if let Some(code_action) = suggested_fix {
-                    fixes.push(code_action);
-                }
+            MappedRustChildDiagnostic::SubDiagnostic(sub) => {
+                subdiagnostics.push(sub);
             }
             MappedRustChildDiagnostic::MessageLine(message_line) => {
                 format_to!(message, "\n{}", message_line);
@@ -284,7 +282,7 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
                 diagnostics.push(MappedRustDiagnostic {
                     url: in_macro_location.uri,
                     diagnostic,
-                    fixes: fixes.clone(),
+                    fixes: Vec::new(),
                 });
             }
 
@@ -298,17 +296,20 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
                     code_description: code_description.clone(),
                     source: Some(source.clone()),
                     message,
-                    related_information: if related_information.is_empty() {
+                    related_information: if subdiagnostics.is_empty() {
                         None
                     } else {
-                        let mut related = related_information.clone();
+                        let mut related = subdiagnostics
+                            .iter()
+                            .map(|sub| sub.related.clone())
+                            .collect::<Vec<_>>();
                         related.extend(related_macro_info);
                         Some(related)
                     },
                     tags: if tags.is_empty() { None } else { Some(tags.clone()) },
                     data: None,
                 },
-                fixes: fixes.clone(),
+                fixes: Vec::new(),
             });
 
             // Emit hint-level diagnostics for all `related_information` entries such as "help"s.
@@ -318,21 +319,21 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
                 location,
                 message: "original diagnostic".to_string(),
             };
-            for info in &related_information {
+            for sub in &subdiagnostics {
                 // Filter out empty/non-existent messages, as they greatly confuse VS Code.
-                if info.message.is_empty() {
+                if sub.related.message.is_empty() {
                     continue;
                 }
                 diagnostics.push(MappedRustDiagnostic {
-                    url: info.location.uri.clone(),
-                    fixes: fixes.clone(), // share fixes to make them easier to apply
+                    url: sub.related.location.uri.clone(),
+                    fixes: sub.suggested_fix.iter().cloned().collect(),
                     diagnostic: lsp_types::Diagnostic {
-                        range: info.location.range,
+                        range: sub.related.location.range,
                         severity: Some(lsp_types::DiagnosticSeverity::Hint),
                         code: code.clone().map(lsp_types::NumberOrString::String),
                         code_description: code_description.clone(),
                         source: Some(source.clone()),
-                        message: info.message.clone(),
+                        message: sub.related.message.clone(),
                         related_information: Some(vec![back_ref.clone()]),
                         tags: None, // don't apply modifiers again
                         data: None,