Skip to content

Commit 8844640

Browse files
committed
fix: Put style lints behind disabled-by-default config
1 parent ce3216e commit 8844640

File tree

7 files changed

+66
-20
lines changed

7 files changed

+66
-20
lines changed

crates/hir-ty/src/diagnostics/expr.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,17 @@ pub enum BodyValidationDiagnostic {
6060
}
6161

6262
impl BodyValidationDiagnostic {
63-
pub fn collect(db: &dyn HirDatabase, owner: DefWithBodyId) -> Vec<BodyValidationDiagnostic> {
63+
pub fn collect(
64+
db: &dyn HirDatabase,
65+
owner: DefWithBodyId,
66+
validate_lints: bool,
67+
) -> Vec<BodyValidationDiagnostic> {
6468
let _p =
6569
tracing::span!(tracing::Level::INFO, "BodyValidationDiagnostic::collect").entered();
6670
let infer = db.infer(owner);
6771
let body = db.body(owner);
68-
let mut validator = ExprValidator { owner, body, infer, diagnostics: Vec::new() };
72+
let mut validator =
73+
ExprValidator { owner, body, infer, diagnostics: Vec::new(), validate_lints };
6974
validator.validate_body(db);
7075
validator.diagnostics
7176
}
@@ -76,6 +81,7 @@ struct ExprValidator {
7681
body: Arc<Body>,
7782
infer: Arc<InferenceResult>,
7883
diagnostics: Vec<BodyValidationDiagnostic>,
84+
validate_lints: bool,
7985
}
8086

8187
impl ExprValidator {
@@ -139,6 +145,9 @@ impl ExprValidator {
139145
expr: &Expr,
140146
filter_map_next_checker: &mut Option<FilterMapNextChecker>,
141147
) {
148+
if !self.validate_lints {
149+
return;
150+
}
142151
// Check that the number of arguments matches the number of parameters.
143152

144153
if self.infer.expr_type_mismatches().next().is_some() {
@@ -308,6 +317,9 @@ impl ExprValidator {
308317
}
309318

310319
fn check_for_trailing_return(&mut self, body_expr: ExprId, body: &Body) {
320+
if !self.validate_lints {
321+
return;
322+
}
311323
match &body.exprs[body_expr] {
312324
Expr::Block { statements, tail, .. } => {
313325
let last_stmt = tail.or_else(|| match statements.last()? {
@@ -340,6 +352,9 @@ impl ExprValidator {
340352
}
341353

342354
fn check_for_unnecessary_else(&mut self, id: ExprId, expr: &Expr, db: &dyn HirDatabase) {
355+
if !self.validate_lints {
356+
return;
357+
}
343358
if let Expr::If { condition: _, then_branch, else_branch } = expr {
344359
if else_branch.is_none() {
345360
return;

crates/hir/src/lib.rs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ impl ModuleDef {
365365
Some(name)
366366
}
367367

368-
pub fn diagnostics(self, db: &dyn HirDatabase) -> Vec<AnyDiagnostic> {
368+
pub fn diagnostics(self, db: &dyn HirDatabase, style_lints: bool) -> Vec<AnyDiagnostic> {
369369
let id = match self {
370370
ModuleDef::Adt(it) => match it {
371371
Adt::Struct(it) => it.id.into(),
@@ -387,7 +387,7 @@ impl ModuleDef {
387387

388388
match self.as_def_with_body() {
389389
Some(def) => {
390-
def.diagnostics(db, &mut acc);
390+
def.diagnostics(db, &mut acc, style_lints);
391391
}
392392
None => {
393393
for diag in hir_ty::diagnostics::incorrect_case(db, id) {
@@ -541,7 +541,12 @@ impl Module {
541541
}
542542

543543
/// Fills `acc` with the module's diagnostics.
544-
pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
544+
pub fn diagnostics(
545+
self,
546+
db: &dyn HirDatabase,
547+
acc: &mut Vec<AnyDiagnostic>,
548+
style_lints: bool,
549+
) {
545550
let name = self.name(db);
546551
let _p = tracing::span!(tracing::Level::INFO, "Module::diagnostics", ?name);
547552
let def_map = self.id.def_map(db.upcast());
@@ -558,20 +563,20 @@ impl Module {
558563
ModuleDef::Module(m) => {
559564
// Only add diagnostics from inline modules
560565
if def_map[m.id.local_id].origin.is_inline() {
561-
m.diagnostics(db, acc)
566+
m.diagnostics(db, acc, style_lints)
562567
}
563-
acc.extend(def.diagnostics(db))
568+
acc.extend(def.diagnostics(db, style_lints))
564569
}
565570
ModuleDef::Trait(t) => {
566571
for diag in db.trait_data_with_diagnostics(t.id).1.iter() {
567572
emit_def_diagnostic(db, acc, diag);
568573
}
569574

570575
for item in t.items(db) {
571-
item.diagnostics(db, acc);
576+
item.diagnostics(db, acc, style_lints);
572577
}
573578

574-
acc.extend(def.diagnostics(db))
579+
acc.extend(def.diagnostics(db, style_lints))
575580
}
576581
ModuleDef::Adt(adt) => {
577582
match adt {
@@ -587,17 +592,17 @@ impl Module {
587592
}
588593
Adt::Enum(e) => {
589594
for v in e.variants(db) {
590-
acc.extend(ModuleDef::Variant(v).diagnostics(db));
595+
acc.extend(ModuleDef::Variant(v).diagnostics(db, style_lints));
591596
for diag in db.enum_variant_data_with_diagnostics(v.id).1.iter() {
592597
emit_def_diagnostic(db, acc, diag);
593598
}
594599
}
595600
}
596601
}
597-
acc.extend(def.diagnostics(db))
602+
acc.extend(def.diagnostics(db, style_lints))
598603
}
599604
ModuleDef::Macro(m) => emit_macro_def_diagnostics(db, acc, m),
600-
_ => acc.extend(def.diagnostics(db)),
605+
_ => acc.extend(def.diagnostics(db, style_lints)),
601606
}
602607
}
603608
self.legacy_macros(db).into_iter().for_each(|m| emit_macro_def_diagnostics(db, acc, m));
@@ -738,7 +743,7 @@ impl Module {
738743
}
739744

740745
for &item in &db.impl_data(impl_def.id).items {
741-
AssocItem::from(item).diagnostics(db, acc);
746+
AssocItem::from(item).diagnostics(db, acc, style_lints);
742747
}
743748
}
744749
}
@@ -1616,14 +1621,19 @@ impl DefWithBody {
16161621
}
16171622
}
16181623

1619-
pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
1624+
pub fn diagnostics(
1625+
self,
1626+
db: &dyn HirDatabase,
1627+
acc: &mut Vec<AnyDiagnostic>,
1628+
style_lints: bool,
1629+
) {
16201630
db.unwind_if_cancelled();
16211631
let krate = self.module(db).id.krate();
16221632

16231633
let (body, source_map) = db.body_with_source_map(self.into());
16241634

16251635
for (_, def_map) in body.blocks(db.upcast()) {
1626-
Module { id: def_map.module_id(DefMap::ROOT) }.diagnostics(db, acc);
1636+
Module { id: def_map.module_id(DefMap::ROOT) }.diagnostics(db, acc, style_lints);
16271637
}
16281638

16291639
for diag in source_map.diagnostics() {
@@ -1784,7 +1794,7 @@ impl DefWithBody {
17841794
}
17851795
}
17861796

1787-
for diagnostic in BodyValidationDiagnostic::collect(db, self.into()) {
1797+
for diagnostic in BodyValidationDiagnostic::collect(db, self.into(), style_lints) {
17881798
acc.extend(AnyDiagnostic::body_validation_diagnostic(db, diagnostic, &source_map));
17891799
}
17901800

@@ -2897,13 +2907,18 @@ impl AssocItem {
28972907
}
28982908
}
28992909

2900-
pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
2910+
pub fn diagnostics(
2911+
self,
2912+
db: &dyn HirDatabase,
2913+
acc: &mut Vec<AnyDiagnostic>,
2914+
style_lints: bool,
2915+
) {
29012916
match self {
29022917
AssocItem::Function(func) => {
2903-
DefWithBody::from(func).diagnostics(db, acc);
2918+
DefWithBody::from(func).diagnostics(db, acc, style_lints);
29042919
}
29052920
AssocItem::Const(const_) => {
2906-
DefWithBody::from(const_).diagnostics(db, acc);
2921+
DefWithBody::from(const_).diagnostics(db, acc, style_lints);
29072922
}
29082923
AssocItem::TypeAlias(type_alias) => {
29092924
for diag in hir_ty::diagnostics::incorrect_case(db, type_alias.id.into()) {

crates/ide-diagnostics/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ pub struct DiagnosticsConfig {
227227
pub disable_experimental: bool,
228228
pub disabled: FxHashSet<String>,
229229
pub expr_fill_default: ExprFillDefaultMode,
230+
pub style_lints: bool,
230231
// FIXME: We may want to include a whole `AssistConfig` here
231232
pub insert_use: InsertUseConfig,
232233
pub prefer_no_std: bool,
@@ -245,6 +246,7 @@ impl DiagnosticsConfig {
245246
disable_experimental: Default::default(),
246247
disabled: Default::default(),
247248
expr_fill_default: Default::default(),
249+
style_lints: true,
248250
insert_use: InsertUseConfig {
249251
granularity: ImportGranularity::Preserve,
250252
enforce_granularity: false,
@@ -324,7 +326,7 @@ pub fn diagnostics(
324326

325327
let mut diags = Vec::new();
326328
if let Some(m) = module {
327-
m.diagnostics(db, &mut diags);
329+
m.diagnostics(db, &mut diags, config.style_lints);
328330
}
329331

330332
for diag in diags {

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,7 @@ impl flags::AnalysisStats {
982982
},
983983
prefer_no_std: false,
984984
prefer_prelude: true,
985+
style_lints: false,
985986
},
986987
ide::AssistResolveStrategy::All,
987988
file_id,

crates/rust-analyzer/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ config_data! {
311311
/// Map of prefixes to be substituted when parsing diagnostic file paths.
312312
/// This should be the reverse mapping of what is passed to `rustc` as `--remap-path-prefix`.
313313
diagnostics_remapPrefix: FxHashMap<String, String> = "{}",
314+
/// Whether to run additional style lints.
315+
diagnostics_styleLints_enable: bool = "false",
314316
/// List of warnings that should be displayed with hint severity.
315317
///
316318
/// The warnings will be indicated by faded text or three dots in code
@@ -1162,6 +1164,7 @@ impl Config {
11621164
insert_use: self.insert_use_config(),
11631165
prefer_no_std: self.data.imports_preferNoStd,
11641166
prefer_prelude: self.data.imports_preferPrelude,
1167+
style_lints: self.data.diagnostics_styleLints_enable,
11651168
}
11661169
}
11671170

docs/user/generated_config.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,11 @@ have more false positives than usual.
386386
Map of prefixes to be substituted when parsing diagnostic file paths.
387387
This should be the reverse mapping of what is passed to `rustc` as `--remap-path-prefix`.
388388
--
389+
[[rust-analyzer.diagnostics.styleLints.enable]]rust-analyzer.diagnostics.styleLints.enable (default: `false`)::
390+
+
391+
--
392+
Whether to run additional style lints.
393+
--
389394
[[rust-analyzer.diagnostics.warningsAsHint]]rust-analyzer.diagnostics.warningsAsHint (default: `[]`)::
390395
+
391396
--

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,11 @@
948948
"default": {},
949949
"type": "object"
950950
},
951+
"rust-analyzer.diagnostics.styleLints.enable": {
952+
"markdownDescription": "Whether to run additional style lints.",
953+
"default": false,
954+
"type": "boolean"
955+
},
951956
"rust-analyzer.diagnostics.warningsAsHint": {
952957
"markdownDescription": "List of warnings that should be displayed with hint severity.\n\nThe warnings will be indicated by faded text or three dots in code\nand will not show up in the `Problems Panel`.",
953958
"default": [],

0 commit comments

Comments
 (0)