From 831e3d58b32ad64329f0c84ac93b7b97c7d6c268 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Wed, 12 Aug 2020 15:33:07 +0300 Subject: Replace String with &'static str --- crates/ra_hir_def/src/diagnostics.rs | 4 ++-- crates/ra_hir_expand/src/diagnostics.rs | 2 +- crates/ra_hir_ty/src/diagnostics.rs | 32 ++++++++++++++++---------------- crates/ra_ide/src/diagnostics.rs | 26 ++++++++++++-------------- 4 files changed, 31 insertions(+), 33 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_def/src/diagnostics.rs b/crates/ra_hir_def/src/diagnostics.rs index 1d28c24e8..481b13a87 100644 --- a/crates/ra_hir_def/src/diagnostics.rs +++ b/crates/ra_hir_def/src/diagnostics.rs @@ -15,8 +15,8 @@ pub struct UnresolvedModule { } impl Diagnostic for UnresolvedModule { - fn name(&self) -> String { - "unresolved-module".to_string() + fn name(&self) -> &'static str { + "unresolved-module" } fn message(&self) -> String { "unresolved module".to_string() diff --git a/crates/ra_hir_expand/src/diagnostics.rs b/crates/ra_hir_expand/src/diagnostics.rs index ef1d61144..507132a13 100644 --- a/crates/ra_hir_expand/src/diagnostics.rs +++ b/crates/ra_hir_expand/src/diagnostics.rs @@ -21,7 +21,7 @@ use ra_syntax::{SyntaxNode, SyntaxNodePtr}; use crate::{db::AstDatabase, InFile}; pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { - fn name(&self) -> String; + fn name(&self) -> &'static str; fn message(&self) -> String; fn source(&self) -> InFile; fn as_any(&self) -> &(dyn Any + Send + 'static); diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs index 0b3e16ae7..56acd3bbf 100644 --- a/crates/ra_hir_ty/src/diagnostics.rs +++ b/crates/ra_hir_ty/src/diagnostics.rs @@ -33,8 +33,8 @@ pub struct NoSuchField { } impl Diagnostic for NoSuchField { - fn name(&self) -> String { - "no-such-field".to_string() + fn name(&self) -> &'static str { + "no-such-field" } fn message(&self) -> String { @@ -68,8 +68,8 @@ pub struct MissingFields { } impl Diagnostic for MissingFields { - fn name(&self) -> String { - "missing-structure-fields".to_string() + fn name(&self) -> &'static str { + "missing-structure-fields" } fn message(&self) -> String { let mut buf = String::from("Missing structure fields:\n"); @@ -104,8 +104,8 @@ pub struct MissingPatFields { } impl Diagnostic for MissingPatFields { - fn name(&self) -> String { - "missing-pat-fields".to_string() + fn name(&self) -> &'static str { + "missing-pat-fields" } fn message(&self) -> String { let mut buf = String::from("Missing structure fields:\n"); @@ -130,8 +130,8 @@ pub struct MissingMatchArms { } impl Diagnostic for MissingMatchArms { - fn name(&self) -> String { - "missing-match-arm".to_string() + fn name(&self) -> &'static str { + "missing-match-arm" } fn message(&self) -> String { String::from("Missing match arm") @@ -151,8 +151,8 @@ pub struct MissingOkInTailExpr { } impl Diagnostic for MissingOkInTailExpr { - fn name(&self) -> String { - "missing-ok-in-tail-expr".to_string() + fn name(&self) -> &'static str { + "missing-ok-in-tail-expr" } fn message(&self) -> String { "wrap return expression in Ok".to_string() @@ -182,8 +182,8 @@ pub struct BreakOutsideOfLoop { } impl Diagnostic for BreakOutsideOfLoop { - fn name(&self) -> String { - "break-outside-of-loop".to_string() + fn name(&self) -> &'static str { + "break-outside-of-loop" } fn message(&self) -> String { "break outside of loop".to_string() @@ -213,8 +213,8 @@ pub struct MissingUnsafe { } impl Diagnostic for MissingUnsafe { - fn name(&self) -> String { - "missing-unsafe".to_string() + fn name(&self) -> &'static str { + "missing-unsafe" } fn message(&self) -> String { format!("This operation is unsafe and requires an unsafe function or block") @@ -246,8 +246,8 @@ pub struct MismatchedArgCount { } impl Diagnostic for MismatchedArgCount { - fn name(&self) -> String { - "mismatched-arg-count".to_string() + fn name(&self) -> &'static str { + "mismatched-arg-count" } fn message(&self) -> String { let s = if self.expected == 1 { "" } else { "s" }; diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index 8e011a40d..d97bde939 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -63,7 +63,7 @@ pub(crate) fn diagnostics( .into(), ); res.borrow_mut().push(Diagnostic { - name: Some(d.name()), + name: Some(d.name().into()), range: sema.diagnostics_range(d).range, message: d.message(), severity: Severity::Error, @@ -98,7 +98,7 @@ pub(crate) fn diagnostics( }; res.borrow_mut().push(Diagnostic { - name: Some(d.name()), + name: Some(d.name().into()), range: sema.diagnostics_range(d).range, message: d.message(), severity: Severity::Error, @@ -112,7 +112,7 @@ pub(crate) fn diagnostics( let source_change = SourceFileEdit { file_id, edit }.into(); let fix = Fix::new("Wrap with ok", source_change); res.borrow_mut().push(Diagnostic { - name: Some(d.name()), + name: Some(d.name().into()), range: sema.diagnostics_range(d).range, message: d.message(), severity: Severity::Error, @@ -121,7 +121,7 @@ pub(crate) fn diagnostics( }) .on::(|d| { res.borrow_mut().push(Diagnostic { - name: Some(d.name()), + name: Some(d.name().into()), range: sema.diagnostics_range(d).range, message: d.message(), severity: Severity::Error, @@ -133,8 +133,8 @@ pub(crate) fn diagnostics( if !analysis_config.disabled_diagnostics.is_empty() { // Do not collect disabled diagnostics. - sink_builder = sink_builder - .filter(|diag| !analysis_config.disabled_diagnostics.contains(&diag.name())); + sink_builder = + sink_builder.filter(|diag| !analysis_config.disabled_diagnostics.contains(diag.name())); } // Finalize the `DiagnosticSink` building process. @@ -142,7 +142,7 @@ pub(crate) fn diagnostics( // Diagnostics not handled above get no fix and default treatment. .build(|d| { res.borrow_mut().push(Diagnostic { - name: Some(d.name()), + name: Some(d.name().into()), message: d.message(), range: sema.diagnostics_range(d).range, severity: Severity::Error, @@ -313,6 +313,7 @@ fn check_struct_shorthand_initialization( #[cfg(test)] mod tests { + use std::collections::HashSet; use stdx::trim_indent; use test_utils::assert_eq_text; @@ -385,12 +386,9 @@ mod tests { /// Takes a multi-file input fixture with annotated cursor position and the list of disabled diagnostics, /// and checks that provided diagnostics aren't spawned during analysis. - fn check_disabled_diagnostics( - ra_fixture: &str, - disabled_diagnostics: impl IntoIterator, - ) { - let disabled_diagnostics: std::collections::HashSet<_> = - disabled_diagnostics.into_iter().collect(); + fn check_disabled_diagnostics(ra_fixture: &str, disabled_diagnostics: &[&'static str]) { + let disabled_diagnostics: HashSet<_> = + disabled_diagnostics.into_iter().map(|diag| diag.to_string()).collect(); let mock = MockAnalysis::with_files(ra_fixture); let files = mock.files().map(|(it, _)| it).collect::>(); @@ -871,6 +869,6 @@ struct Foo { #[test] fn test_disabled_diagnostics() { - check_disabled_diagnostics(r#"mod foo;"#, vec!["unresolved-module".to_string()]); + check_disabled_diagnostics(r#"mod foo;"#, &vec!["unresolved-module"]); } } -- cgit v1.2.3