Skip to content

Commit

Permalink
Add UseRange to DiagnosticTemplate
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alanz authored and facebook-github-bot committed Jul 12, 2024
1 parent 00418bc commit e0e969e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
22 changes: 16 additions & 6 deletions crates/ide/src/codemod_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>) -> Option<Diagnostic>;

#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum UseRange {
WithArgs,
#[allow(dead_code)]
NameOnly,
}

pub(crate) fn find_call_in_function<T, U>(
diags: &mut Vec<Diagnostic>,
sema: &Semantic,
Expand Down Expand Up @@ -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;
Expand All @@ -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<Diagnostic>,
sema: &Semantic,
Expand Down
15 changes: 11 additions & 4 deletions crates/ide/src/diagnostics/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// ---------------------------------------------------------------------
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions crates/ide/src/diagnostics/slow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]),
Expand All @@ -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")],
},
Expand Down

0 comments on commit e0e969e

Please sign in to comment.