Skip to content

Commit 3b4e355

Browse files
Rollup merge of rust-lang#153227 - kpreid:struct-missing-field, r=estebank
Don’t report missing fields in struct exprs with syntax errors. @Noratrieb [told me](https://internals.rust-lang.org/t/custom-cargo-command-to-show-only-errors-avoid-setting-rustflags-every-time/24032/7?u=kpreid) that “it is a bug if this recovery causes follow-up errors that would not be there if the user fixed the first error.” So, here’s a contribution to hide a follow-up error that annoyed me recently. Specifically, if the user writes a struct literal with a syntax error, such as ```rust StructName { foo: 1 bar: 2 } ``` the compiler will no longer report that the field `bar` is missing in addition to the syntax error. This is my first time attempting any change to the parser or AST; please let me know if there is a better way to do what I’ve done here. ~~The part I’m least happy with is the blast radius of adding another field to `hir::ExprKind::Struct`, but this seems to be in line with the style of the rest of the code. (If this were my own code, I would consider changing `hir::ExprKind::Struct` to a nested struct, the same way it is in `ast::ExprKind`.)~~ The additional information is now stored as an additional variant of `ast::StructRest` / `hir::StructTailExpr`. **Note to reviewers:** I recommend reviewing each commit separately, and in the case of the first one with indentation changes ignored.
2 parents af041c6 + fa94367 commit 3b4e355

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

clippy_lints/src/no_effect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
242242
!expr_ty_has_significant_drop(cx, expr)
243243
&& fields.iter().all(|field| has_no_effect(cx, field.expr))
244244
&& match &base {
245-
StructTailExpr::None | StructTailExpr::DefaultFields(_) => true,
245+
StructTailExpr::None | StructTailExpr::NoneWithError(_) | StructTailExpr::DefaultFields(_) => true,
246246
StructTailExpr::Base(base) => has_no_effect(cx, base),
247247
}
248248
},
@@ -353,7 +353,7 @@ fn reduce_expression<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<Vec
353353
} else {
354354
let base = match base {
355355
StructTailExpr::Base(base) => Some(base),
356-
StructTailExpr::None | StructTailExpr::DefaultFields(_) => None,
356+
StructTailExpr::None | StructTailExpr::NoneWithError(_) | StructTailExpr::DefaultFields(_) => None,
357357
};
358358
Some(fields.iter().map(|f| &f.expr).chain(base).map(Deref::deref).collect())
359359
}

clippy_lints/src/utils/author.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
673673
bind!(self, qpath, fields);
674674
let base = OptionPat::new(match base {
675675
StructTailExpr::Base(base) => Some(self.bind("base", base)),
676-
StructTailExpr::None | StructTailExpr::DefaultFields(_) => None,
676+
StructTailExpr::None | StructTailExpr::NoneWithError(_) | StructTailExpr::DefaultFields(_) => None,
677677
});
678678
kind!("Struct({qpath}, {fields}, {base})");
679679
self.qpath(qpath, &expr.name, expr.value.hir_id);

0 commit comments

Comments
 (0)