From e0e969e9da4412b0431feef26d7aad4f13ab9af9 Mon Sep 17 00:00:00 2001 From: Alan Zimmerman Date: Fri, 12 Jul 2024 02:05:35 -0700 Subject: [PATCH] Add UseRange to DiagnosticTemplate Summary: We can supply a `DiagnosticTemplate` for `find_call_in_function`. Add a `UseRange` field to it, populating existing instances to `UseRange::WithArgs` to match the current behaviour. A subsequent diff will change this (and remove the dead code warnings) Reviewed By: jcpetruzza Differential Revision: D59638550 fbshipit-source-id: bfd641846ebeb05436a067ed94878bdad96aee1d --- crates/ide/src/codemod_helpers.rs | 22 ++++++++++++++------ crates/ide/src/diagnostics/helpers.rs | 15 +++++++++---- crates/ide/src/diagnostics/slow_functions.rs | 3 +++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/crates/ide/src/codemod_helpers.rs b/crates/ide/src/codemod_helpers.rs index d156f8baff..da413097be 100644 --- a/crates/ide/src/codemod_helpers.rs +++ b/crates/ide/src/codemod_helpers.rs @@ -466,10 +466,25 @@ impl<'a, U> MakeDiagCtx<'a, U> { pub fn range_mf_only(&self) -> TextRange { self.range_mf_only.unwrap_or(self.range) } + + #[allow(dead_code)] + pub fn range(&self, use_range: &UseRange) -> TextRange { + match use_range { + UseRange::WithArgs => self.range, + UseRange::NameOnly => self.range_mf_only(), + } + } } pub type MakeDiag<'a, T> = &'a dyn Fn(MakeDiagCtx) -> Option; +#[derive(Eq, PartialEq, Copy, Clone, Debug)] +pub enum UseRange { + WithArgs, + #[allow(dead_code)] + NameOnly, +} + pub(crate) fn find_call_in_function( diags: &mut Vec, sema: &Semantic, @@ -551,6 +566,7 @@ mod tests { use super::find_call_in_function; use super::FunctionMatch; use super::MakeDiagCtx; + use super::UseRange; use crate::diagnostics::Diagnostic; use crate::diagnostics::DiagnosticCode; use crate::diagnostics::DiagnosticsConfig; @@ -560,12 +576,6 @@ mod tests { use crate::tests::check_fix_with_config; use crate::AnalysisHost; - #[derive(Eq, PartialEq, Copy, Clone, Debug)] - enum UseRange { - WithArgs, - NameOnly, - } - fn check_functions( diags: &mut Vec, sema: &Semantic, diff --git a/crates/ide/src/diagnostics/helpers.rs b/crates/ide/src/diagnostics/helpers.rs index 40294c9054..884d00c905 100644 --- a/crates/ide/src/diagnostics/helpers.rs +++ b/crates/ide/src/diagnostics/helpers.rs @@ -18,6 +18,7 @@ use super::Diagnostic; use super::Severity; use crate::codemod_helpers::find_call_in_function; use crate::codemod_helpers::MakeDiagCtx; +use crate::codemod_helpers::UseRange; use crate::FunctionMatch; // --------------------------------------------------------------------- @@ -28,6 +29,7 @@ pub(crate) struct DiagnosticTemplate { pub(crate) message: String, pub(crate) severity: Severity, pub(crate) with_ignore_fix: bool, + pub(crate) use_range: UseRange, } /// Define a checker for a function that should not be used. Generate @@ -65,15 +67,18 @@ pub(crate) fn check_function_with_diagnostic_template( def, mfas, &move |ctx| Some(*ctx.t), - &move |MakeDiagCtx { + &move |ctx @ MakeDiagCtx { sema, def_fb, extra, - range, .. }: MakeDiagCtx<'_, &DiagnosticTemplate>| { - let diag = Diagnostic::new(extra.code.clone(), extra.message.clone(), range) - .with_severity(extra.severity); + let diag = Diagnostic::new( + extra.code.clone(), + extra.message.clone(), + ctx.range(&extra.use_range), + ) + .with_severity(extra.severity); let diag = if extra.with_ignore_fix { diag.with_ignore_fix(sema, def_fb.file_id()) } else { @@ -93,6 +98,7 @@ mod tests { use super::check_used_functions; use super::DiagnosticTemplate; use super::FunctionCallDiagnostic; + use crate::codemod_helpers::UseRange; use crate::diagnostics::AdhocSemanticDiagnostics; use crate::diagnostics::DiagnosticsConfig; use crate::diagnostics::Severity; @@ -124,6 +130,7 @@ mod tests { message: "diagnostic message".to_string(), severity: Severity::Warning, with_ignore_fix: true, + use_range: UseRange::WithArgs, }, matches: vec![FunctionMatch::mfas("main", "foo", vec![0])] .into_iter() diff --git a/crates/ide/src/diagnostics/slow_functions.rs b/crates/ide/src/diagnostics/slow_functions.rs index 109bff9cfb..4287e001df 100644 --- a/crates/ide/src/diagnostics/slow_functions.rs +++ b/crates/ide/src/diagnostics/slow_functions.rs @@ -21,6 +21,7 @@ use super::Diagnostic; use super::DiagnosticConditions; use super::DiagnosticDescriptor; use crate::codemod_helpers::FunctionMatch; +use crate::codemod_helpers::UseRange; use crate::diagnostics::helpers::FunctionCallDiagnostic; use crate::diagnostics::DiagnosticCode; use crate::diagnostics::Severity; @@ -58,6 +59,7 @@ See https://www.erlang.org/doc/man/sets.html .to_string(), severity: Severity::Warning, with_ignore_fix: true, + use_range: UseRange::WithArgs }, matches: vec![ FunctionMatch::mfas("sets", "new", vec![0]), @@ -77,6 +79,7 @@ See https://www.erlang.org/doc/man/sets.html .to_string(), severity: Severity::Warning, with_ignore_fix: true, + use_range: UseRange::WithArgs }, matches: vec![FunctionMatch::m("dict")], },