Replace String with &'static str

This commit is contained in:
Igor Aleksanov 2020-08-12 15:33:07 +03:00
parent c51fb7aca5
commit 831e3d58b3
4 changed files with 31 additions and 33 deletions

View File

@ -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()

View File

@ -21,7 +21,7 @@
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<SyntaxNodePtr>;
fn as_any(&self) -> &(dyn Any + Send + 'static);

View File

@ -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" };

View File

@ -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::<hir::diagnostics::NoSuchField, _>(|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 @@ fn check_no_diagnostics(ra_fixture: &str) {
/// 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<Item = String>,
) {
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::<Vec<_>>();
@ -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"]);
}
}