Skip to content

Commit 1984079

Browse files
committed
lint to change numeric literal instead of into
1 parent e4ba1d4 commit 1984079

File tree

1 file changed

+72
-18
lines changed

1 file changed

+72
-18
lines changed

src/librustc_typeck/check/demand.rs

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,21 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
419419
if needs_paren { "(" } else { "" },
420420
src,
421421
if needs_paren { ")" } else { "" });
422+
let suffix_suggestion = format!(
423+
"{}{}{}{}",
424+
if needs_paren { "(" } else { "" },
425+
src,
426+
expected_ty,
427+
if needs_paren { ")" } else { "" },
428+
);
429+
430+
let is_suffixed = |expr: &hir::Expr| {
431+
if let hir::ExprKind::Lit(lit) = &expr.node {
432+
lit.node.is_suffixed()
433+
} else {
434+
false
435+
}
436+
};
422437

423438
match (&expected_ty.sty, &checked_ty.sty) {
424439
(&ty::Int(ref exp), &ty::Int(ref found)) => {
@@ -444,12 +459,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
444459
}
445460
}
446461
_ => {
447-
err.span_suggestion_with_applicability(
448-
expr.span,
449-
&format!("{}, which {}", msg, will_sign_extend),
450-
into_suggestion,
451-
Applicability::MachineApplicable
452-
);
462+
if is_suffixed(expr) {
463+
err.span_suggestion_with_applicability(
464+
expr.span,
465+
&format!(
466+
"change the type of the numeric literal from `{}` to `{}`",
467+
checked_ty,
468+
expected_ty,
469+
),
470+
suffix_suggestion,
471+
Applicability::MaybeIncorrect,
472+
);
473+
} else {
474+
err.span_suggestion_with_applicability(
475+
expr.span,
476+
&format!("{}, which {}", msg, will_sign_extend),
477+
into_suggestion,
478+
Applicability::MachineApplicable
479+
);
480+
}
453481
}
454482
}
455483
true
@@ -477,12 +505,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
477505
}
478506
}
479507
_ => {
480-
err.span_suggestion_with_applicability(
481-
expr.span,
482-
&format!("{}, which {}", msg, will_zero_extend),
483-
into_suggestion,
484-
Applicability::MachineApplicable
485-
);
508+
if is_suffixed(expr) {
509+
err.span_suggestion_with_applicability(
510+
expr.span,
511+
&format!(
512+
"change the type of the numeric literal from `{}` to `{}`",
513+
checked_ty,
514+
expected_ty,
515+
),
516+
suffix_suggestion,
517+
Applicability::MaybeIncorrect,
518+
);
519+
} else {
520+
err.span_suggestion_with_applicability(
521+
expr.span,
522+
&format!("{}, which {}", msg, will_zero_extend),
523+
into_suggestion,
524+
Applicability::MachineApplicable
525+
);
526+
}
486527
}
487528
}
488529
true
@@ -583,12 +624,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
583624
}
584625
(&ty::Float(ref exp), &ty::Float(ref found)) => {
585626
if found.bit_width() < exp.bit_width() {
586-
err.span_suggestion_with_applicability(
587-
expr.span,
588-
&format!("{} in a lossless way", msg),
589-
into_suggestion,
590-
Applicability::MachineApplicable
591-
);
627+
if is_suffixed(expr) {
628+
err.span_suggestion_with_applicability(
629+
expr.span,
630+
&format!(
631+
"change the type of the numeric literal from `{}` to `{}`",
632+
checked_ty,
633+
expected_ty,
634+
),
635+
suffix_suggestion,
636+
Applicability::MaybeIncorrect,
637+
);
638+
} else {
639+
err.span_suggestion_with_applicability(
640+
expr.span,
641+
&format!("{} in a lossless way", msg),
642+
into_suggestion,
643+
Applicability::MachineApplicable
644+
);
645+
}
592646
} else if can_cast {
593647
err.span_suggestion_with_applicability(
594648
expr.span,

0 commit comments

Comments
 (0)