Skip to content

Commit

Permalink
Make lint store its severity
Browse files Browse the repository at this point in the history
Summary: Rather than having is_serious with unclear semantics, just store a severity level directly.

Reviewed By: stepancheg

Differential Revision: D47150377

fbshipit-source-id: 156fddb540d56b129a74db51190aa539fed572c4
  • Loading branch information
ndmitchell authored and facebook-github-bot committed Jul 5, 2023
1 parent 7b3c041 commit f25cfc5
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 24 deletions.
3 changes: 2 additions & 1 deletion app/buck2_starlark/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use dice::DiceTransaction;
use dupe::Dupe;
use starlark::codemap::FileSpan;
use starlark::errors::Diagnostic;
use starlark::errors::EvalSeverity;
use starlark::errors::Lint;
use starlark::syntax::AstModule;

Expand Down Expand Up @@ -104,7 +105,7 @@ async fn lint_file(
Ok(vec![Lint {
location: span.unwrap_or_else(|| FileSpan::new(path_str, content)),
short_name: "parse_error".to_owned(),
serious: true,
severity: EvalSeverity::Error,
problem: format!("{:#}", message),
original: "".to_owned(),
}])
Expand Down
5 changes: 3 additions & 2 deletions starlark-rust/starlark/src/analysis/dubious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use thiserror::Error;

use crate::analysis::types::LintT;
use crate::analysis::types::LintWarning;
use crate::analysis::EvalSeverity;
use crate::codemap::CodeMap;
use crate::codemap::FileSpan;
use crate::codemap::Span;
Expand All @@ -43,8 +44,8 @@ pub(crate) enum Dubious {
}

impl LintWarning for Dubious {
fn is_serious(&self) -> bool {
true
fn severity(&self) -> EvalSeverity {
EvalSeverity::Warning
}

fn short_name(&self) -> &'static str {
Expand Down
7 changes: 4 additions & 3 deletions starlark-rust/starlark/src/analysis/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use thiserror::Error;

use crate::analysis::types::LintT;
use crate::analysis::types::LintWarning;
use crate::analysis::EvalSeverity;
use crate::codemap::CodeMap;
use crate::codemap::ResolvedFileSpan;
use crate::codemap::Span;
Expand Down Expand Up @@ -51,11 +52,11 @@ pub(crate) enum FlowIssue {
}

impl LintWarning for FlowIssue {
fn is_serious(&self) -> bool {
fn severity(&self) -> EvalSeverity {
match self {
// Sometimes people add these to make flow clearer
FlowIssue::RedundantContinue | FlowIssue::RedundantReturn => false,
_ => true,
FlowIssue::RedundantContinue | FlowIssue::RedundantReturn => EvalSeverity::Disabled,
_ => EvalSeverity::Warning,
}
}

Expand Down
5 changes: 3 additions & 2 deletions starlark-rust/starlark/src/analysis/incompatible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use thiserror::Error;

use crate::analysis::types::LintT;
use crate::analysis::types::LintWarning;
use crate::analysis::EvalSeverity;
use crate::codemap::CodeMap;
use crate::codemap::FileSpan;
use crate::codemap::Span;
Expand All @@ -46,8 +47,8 @@ pub(crate) enum Incompatibility {
}

impl LintWarning for Incompatibility {
fn is_serious(&self) -> bool {
true
fn severity(&self) -> EvalSeverity {
EvalSeverity::Warning
}

fn short_name(&self) -> &'static str {
Expand Down
7 changes: 4 additions & 3 deletions starlark-rust/starlark/src/analysis/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use thiserror::Error;

use crate::analysis::types::LintT;
use crate::analysis::types::LintWarning;
use crate::analysis::EvalSeverity;
use crate::codemap::CodeMap;
use crate::codemap::Span;
use crate::codemap::Spanned;
Expand Down Expand Up @@ -66,10 +67,10 @@ pub(crate) enum NameWarning {
}

impl LintWarning for NameWarning {
fn is_serious(&self) -> bool {
fn severity(&self) -> EvalSeverity {
match self {
Self::UsingUnassigned(..) => true,
_ => false,
Self::UsingUnassigned(..) => EvalSeverity::Warning,
_ => EvalSeverity::Disabled,
}
}

Expand Down
5 changes: 3 additions & 2 deletions starlark-rust/starlark/src/analysis/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use thiserror::Error;

use crate::analysis::types::LintT;
use crate::analysis::types::LintWarning;
use crate::analysis::EvalSeverity;
use crate::codemap::CodeMap;
use crate::syntax::ast::Argument;
use crate::syntax::ast::AstExpr;
Expand All @@ -40,8 +41,8 @@ pub(crate) enum Performance {
}

impl LintWarning for Performance {
fn is_serious(&self) -> bool {
true
fn severity(&self) -> EvalSeverity {
EvalSeverity::Warning
}

fn short_name(&self) -> &'static str {
Expand Down
13 changes: 4 additions & 9 deletions starlark-rust/starlark/src/analysis/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::codemap::Span;
use crate::errors::Diagnostic;

pub(crate) trait LintWarning: Display {
fn is_serious(&self) -> bool;
fn severity(&self) -> EvalSeverity;
fn short_name(&self) -> &'static str;
}

Expand All @@ -54,7 +54,7 @@ pub struct Lint {
pub short_name: String,
/// Is this code highly-likely to be wrong, rather
/// than merely stylistically non-ideal.
pub serious: bool,
pub severity: EvalSeverity,
/// A description of the underlying problem.
pub problem: String,
/// The source code at [`location`](Lint::location).
Expand Down Expand Up @@ -87,7 +87,7 @@ impl<T: LintWarning> LintT<T> {
Lint {
location: self.location,
short_name: self.problem.short_name().to_owned(),
serious: self.problem.is_serious(),
severity: self.problem.severity(),
problem: self.problem.to_string(),
original: self.original,
}
Expand Down Expand Up @@ -200,12 +200,7 @@ impl From<Lint> for EvalMessage {
Self {
path: x.location.filename().to_owned(),
span: Some(x.location.resolve_span()),
severity: if x.serious {
EvalSeverity::Warning
} else {
// Start with all non-serious errors disabled, and ramp up from there
EvalSeverity::Disabled
},
severity: x.severity,
name: x.short_name,
description: x.problem,
full_error_with_span: None,
Expand Down
5 changes: 3 additions & 2 deletions starlark-rust/starlark/src/analysis/underscore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use thiserror::Error;

use crate::analysis::types::LintT;
use crate::analysis::types::LintWarning;
use crate::analysis::EvalSeverity;
use crate::codemap::CodeMap;
use crate::syntax::ast::Assign;
use crate::syntax::ast::AstExpr;
Expand All @@ -39,8 +40,8 @@ pub(crate) enum UnderscoreWarning {
}

impl LintWarning for UnderscoreWarning {
fn is_serious(&self) -> bool {
false
fn severity(&self) -> EvalSeverity {
EvalSeverity::Disabled
}

fn short_name(&self) -> &'static str {
Expand Down

0 comments on commit f25cfc5

Please sign in to comment.