Skip to content

Commit ffb670c

Browse files
committed
DRY for struct variant used, also use span_suggestion for E0423
1 parent cb9e632 commit ffb670c

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

src/librustc_lint/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ impl LateLintPass for TypeLimits {
350350
"unary negation of unsigned integer",
351351
"E0519");
352352
if let Ok(snip) = cx.sess().codemap().span_to_snippet(lit_span) {
353-
err.span_suggestion(span, "try using a cast or the `!` operator:", snip);
353+
err.span_suggestion(span, "try using a cast or the `!` operator:",
354+
format!("!{}", snip));
354355
} else {
355356
err.span_help(span, "use a cast or the `!` operator");
356357
}

src/librustc_resolve/lib.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,22 @@ fn resolve_struct_error<'b, 'a: 'b, 'tcx: 'a>(resolver: &'b Resolver<'a, 'tcx>,
420420
name)
421421
}
422422
ResolutionError::StructVariantUsedAsFunction(path_name) => {
423-
struct_span_err!(resolver.session,
424-
span,
425-
E0423,
426-
"`{}` is the name of a struct or struct variant, but this expression \
427-
uses it like a function name",
428-
path_name)
423+
let mut err = struct_span_err!(resolver.session,
424+
span,
425+
E0423,
426+
"`{}` is the name of a struct or \
427+
struct variant, but this expression \
428+
uses it like a function name",
429+
path_name);
430+
if resolver.emit_errors {
431+
let msg = format!("did you mean to write: `{} {{ /* fields */ }}`?",
432+
path_name);
433+
err.fileline_help(span, &msg);
434+
} else {
435+
let suggestion = format!("{} {{ /* fields */ }}", path_name);
436+
err.span_suggestion(span, "did you mean to write", suggestion);
437+
}
438+
err
429439
}
430440
ResolutionError::SelfNotAvailableInStaticMethod => {
431441
struct_span_err!(resolver.session,
@@ -3521,18 +3531,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
35213531
if let DefVariant(_, _, true) = path_res.base_def {
35223532
let path_name = path_names_to_string(path, 0);
35233533

3524-
let mut err = resolve_struct_error(self,
3525-
expr.span,
3526-
ResolutionError::StructVariantUsedAsFunction(&*path_name));
3534+
resolve_error(self, expr.span,
3535+
ResolutionError::StructVariantUsedAsFunction(&*path_name));
35273536

3528-
let msg = format!("did you mean to write: `{} {{ /* fields */ }}`?",
3529-
path_name);
3530-
if self.emit_errors {
3531-
err.fileline_help(expr.span, &msg);
3532-
} else {
3533-
err.span_help(expr.span, &msg);
3534-
}
3535-
err.emit();
35363537
self.record_def(expr.id, err_path_resolution());
35373538
} else {
35383539
// Write the result into the def map.
@@ -3562,18 +3563,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
35623563
self.record_def(expr.id, err_path_resolution());
35633564
match type_res.map(|r| r.base_def) {
35643565
Some(DefTy(struct_id, _)) if self.structs.contains_key(&struct_id) => {
3565-
let mut err = resolve_struct_error(self,
3566-
expr.span,
3567-
ResolutionError::StructVariantUsedAsFunction(&*path_name));
3568-
3569-
let msg = format!("did you mean to write: `{} {{ /* fields */ }}`?",
3570-
path_name);
3571-
if self.emit_errors {
3572-
err.fileline_help(expr.span, &msg);
3573-
} else {
3574-
err.span_help(expr.span, &msg);
3575-
}
3576-
err.emit();
3566+
resolve_error(self, expr.span,
3567+
ResolutionError::StructVariantUsedAsFunction(&*path_name));
35773568
}
35783569
_ => {
35793570
// Keep reporting some errors even if they're ignored above.

0 commit comments

Comments
 (0)