fix: Put style lints behind disabled-by-default config

This commit is contained in:
Lukas Wirth 2024-03-05 11:41:46 +01:00
parent ce3216e0ae
commit 8844640c6f
7 changed files with 66 additions and 20 deletions

View file

@ -60,12 +60,17 @@ pub enum BodyValidationDiagnostic {
}
impl BodyValidationDiagnostic {
pub fn collect(db: &dyn HirDatabase, owner: DefWithBodyId) -> Vec<BodyValidationDiagnostic> {
pub fn collect(
db: &dyn HirDatabase,
owner: DefWithBodyId,
validate_lints: bool,
) -> Vec<BodyValidationDiagnostic> {
let _p =
tracing::span!(tracing::Level::INFO, "BodyValidationDiagnostic::collect").entered();
let infer = db.infer(owner);
let body = db.body(owner);
let mut validator = ExprValidator { owner, body, infer, diagnostics: Vec::new() };
let mut validator =
ExprValidator { owner, body, infer, diagnostics: Vec::new(), validate_lints };
validator.validate_body(db);
validator.diagnostics
}
@ -76,6 +81,7 @@ struct ExprValidator {
body: Arc<Body>,
infer: Arc<InferenceResult>,
diagnostics: Vec<BodyValidationDiagnostic>,
validate_lints: bool,
}
impl ExprValidator {
@ -139,6 +145,9 @@ impl ExprValidator {
expr: &Expr,
filter_map_next_checker: &mut Option<FilterMapNextChecker>,
) {
if !self.validate_lints {
return;
}
// Check that the number of arguments matches the number of parameters.
if self.infer.expr_type_mismatches().next().is_some() {
@ -308,6 +317,9 @@ impl ExprValidator {
}
fn check_for_trailing_return(&mut self, body_expr: ExprId, body: &Body) {
if !self.validate_lints {
return;
}
match &body.exprs[body_expr] {
Expr::Block { statements, tail, .. } => {
let last_stmt = tail.or_else(|| match statements.last()? {
@ -340,6 +352,9 @@ impl ExprValidator {
}
fn check_for_unnecessary_else(&mut self, id: ExprId, expr: &Expr, db: &dyn HirDatabase) {
if !self.validate_lints {
return;
}
if let Expr::If { condition: _, then_branch, else_branch } = expr {
if else_branch.is_none() {
return;