cx.struct_span_lint(OVERFLOWING_LITERALS, expr.span, |lint| {
let (t, actually) = match ty {
attr::IntType::SignedInt(t) => {
- let actually = size.sign_extend(val) as i128;
+ let actually = if negative {
+ -(size.sign_extend(val) as i128)
+ } else {
+ size.sign_extend(val) as i128
+ };
(t.name_str(), actually.to_string())
}
attr::IntType::UnsignedInt(t) => {
(t.name_str(), actually.to_string())
}
};
- let mut err = lint.build(&format!("literal out of range for {}", t));
- err.note(&format!(
- "the literal `{}` (decimal `{}`) does not fit into \
- the type `{}` and will become `{}{}`",
- repr_str, val, t, actually, t
- ));
+ let mut err = lint.build(&format!("literal out of range for `{}`", t));
+ if negative {
+ // If the value is negative,
+ // emits a note about the value itself, apart from the literal.
+ err.note(&format!(
+ "the literal `{}` (decimal `{}`) does not fit into \
+ the type `{}`",
+ repr_str, val, t
+ ));
+ err.note(&format!("and the value `-{}` will become `{}{}`", repr_str, actually, t));
+ } else {
+ err.note(&format!(
+ "the literal `{}` (decimal `{}`) does not fit into \
+ the type `{}` and will become `{}{}`",
+ repr_str, val, t, actually, t
+ ));
+ }
if let Some(sugg_ty) =
get_type_suggestion(&cx.typeck_results().node_type(expr.hir_id), val, negative)
{
let (sans_suffix, _) = repr_str.split_at(pos);
err.span_suggestion(
expr.span,
- &format!("consider using `{}` instead", sugg_ty),
+ &format!("consider using the type `{}` instead", sugg_ty),
format!("{}{}", sans_suffix, sugg_ty),
Applicability::MachineApplicable,
);
} else {
- err.help(&format!("consider using `{}` instead", sugg_ty));
+ err.help(&format!("consider using the type `{}` instead", sugg_ty));
}
}
err.emit();
}
cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
- lint.build(&format!("literal out of range for `{}`", t.name_str()))
- .note(&format!(
- "the literal `{}` does not fit into the type `{}` whose range is `{}..={}`",
- cx.sess()
- .source_map()
- .span_to_snippet(lit.span)
- .expect("must get snippet from literal"),
- t.name_str(),
- min,
- max,
- ))
- .emit();
+ let mut err = lint.build(&format!("literal out of range for `{}`", t.name_str()));
+ err.note(&format!(
+ "the literal `{}` does not fit into the type `{}` whose range is `{}..={}`",
+ cx.sess()
+ .source_map()
+ .span_to_snippet(lit.span)
+ .expect("must get snippet from literal"),
+ t.name_str(),
+ min,
+ max,
+ ));
+ if let Some(sugg_ty) =
+ get_type_suggestion(&cx.typeck_results().node_type(e.hir_id), v, negative)
+ {
+ err.help(&format!("consider using the type `{}` instead", sugg_ty));
+ }
+ err.emit();
});
}
}
impl<'tcx> LateLintPass<'tcx> for TypeLimits {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx hir::Expr<'tcx>) {
match e.kind {
- hir::ExprKind::Unary(hir::UnOp::UnNeg, ref expr) => {
+ hir::ExprKind::Unary(hir::UnOp::Neg, ref expr) => {
// propagate negation, if the negation itself isn't negated
if self.negated_expr_id != Some(e.hir_id) {
self.negated_expr_id = Some(expr.hir_id);
}
/// Is type known to be non-null?
-crate fn ty_is_known_nonnull<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, mode: CItemKind) -> bool {
+fn ty_is_known_nonnull<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, mode: CItemKind) -> bool {
let tcx = cx.tcx;
match ty.kind() {
ty::FnPtr(_) => true,
return true;
}
+ // Types with a `#[repr(no_niche)]` attribute have their niche hidden.
+ // The attribute is used by the UnsafeCell for example (the only use so far).
+ if def.repr.hide_niche() {
+ return false;
+ }
+
for variant in &def.variants {
if let Some(field) = transparent_newtype_field(cx.tcx, variant) {
if ty_is_known_nonnull(cx, field.ty(tcx, substs), mode) {
impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDeclarations {
fn check_foreign_item(&mut self, cx: &LateContext<'_>, it: &hir::ForeignItem<'_>) {
let mut vis = ImproperCTypesVisitor { cx, mode: CItemKind::Declaration };
- let abi = cx.tcx.hir().get_foreign_abi(it.hir_id);
+ let abi = cx.tcx.hir().get_foreign_abi(it.hir_id());
if !vis.is_internal_abi(abi) {
match it.kind {
hir::ForeignItemKind::Fn(ref decl, _, _) => {
- vis.check_foreign_fn(it.hir_id, decl);
+ vis.check_foreign_fn(it.hir_id(), decl);
}
hir::ForeignItemKind::Static(ref ty, _) => {
- vis.check_foreign_static(it.hir_id, ty.span);
+ vis.check_foreign_static(it.hir_id(), ty.span);
}
hir::ForeignItemKind::Type => (),
}
impl<'tcx> LateLintPass<'tcx> for VariantSizeDifferences {
fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
if let hir::ItemKind::Enum(ref enum_definition, _) = it.kind {
- let item_def_id = cx.tcx.hir().local_def_id(it.hir_id);
- let t = cx.tcx.type_of(item_def_id);
+ let t = cx.tcx.type_of(it.def_id);
let ty = cx.tcx.erase_regions(t);
let layout = match cx.layout_of(ty) {
Ok(layout) => layout,