Merge pull request #3288 from devonhollowood/pedantic-dogfood-casts

Pedantic dogfood: casts
This commit is contained in:
Philipp Hansch 2018-10-11 08:07:51 +02:00 committed by GitHub
commit 928a6d3dc7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 34 additions and 12 deletions

View file

@ -18,6 +18,7 @@ use crate::rustc::ty::{self, Ty, TyCtxt, Instance};
use crate::rustc::ty::subst::{Subst, Substs};
use std::cmp::Ordering::{self, Equal};
use std::cmp::PartialOrd;
use std::convert::TryInto;
use std::hash::{Hash, Hasher};
use std::mem;
use std::rc::Rc;
@ -229,6 +230,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
}
}
#[allow(clippy::cast_possible_wrap)]
fn constant_not(&self, o: &Constant, ty: ty::Ty<'_>) -> Option<Constant> {
use self::Constant::*;
match *o {
@ -341,8 +343,12 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
BinOpKind::Mul => l.checked_mul(r).map(zext),
BinOpKind::Div if r != 0 => l.checked_div(r).map(zext),
BinOpKind::Rem if r != 0 => l.checked_rem(r).map(zext),
BinOpKind::Shr => l.checked_shr(r as u128 as u32).map(zext),
BinOpKind::Shl => l.checked_shl(r as u128 as u32).map(zext),
BinOpKind::Shr => l.checked_shr(
r.try_into().expect("invalid shift")
).map(zext),
BinOpKind::Shl => l.checked_shl(
r.try_into().expect("invalid shift")
).map(zext),
BinOpKind::BitXor => Some(zext(l ^ r)),
BinOpKind::BitOr => Some(zext(l | r)),
BinOpKind::BitAnd => Some(zext(l & r)),
@ -362,8 +368,12 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
BinOpKind::Mul => l.checked_mul(r).map(Constant::Int),
BinOpKind::Div => l.checked_div(r).map(Constant::Int),
BinOpKind::Rem => l.checked_rem(r).map(Constant::Int),
BinOpKind::Shr => l.checked_shr(r as u32).map(Constant::Int),
BinOpKind::Shl => l.checked_shl(r as u32).map(Constant::Int),
BinOpKind::Shr => l.checked_shr(
r.try_into().expect("shift too large")
).map(Constant::Int),
BinOpKind::Shl => l.checked_shl(
r.try_into().expect("shift too large")
).map(Constant::Int),
BinOpKind::BitXor => Some(Constant::Int(l ^ r)),
BinOpKind::BitOr => Some(Constant::Int(l | r)),
BinOpKind::BitAnd => Some(Constant::Int(l & r)),
@ -426,8 +436,12 @@ pub fn miri_to_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, result: &ty::Const<'
ConstValue::Scalar(Scalar::Bits{ bits: b, ..}) => match result.ty.sty {
ty::Bool => Some(Constant::Bool(b == 1)),
ty::Uint(_) | ty::Int(_) => Some(Constant::Int(b)),
ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(b as u32))),
ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(b as u64))),
ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(
b.try_into().expect("invalid f32 bit representation")
))),
ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(
b.try_into().expect("invalid f64 bit representation")
))),
// FIXME: implement other conversion
_ => None,
},
@ -439,7 +453,7 @@ pub fn miri_to_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, result: &ty::Const<'
.alloc_map
.lock()
.unwrap_memory(ptr.alloc_id);
let offset = ptr.offset.bytes() as usize;
let offset = ptr.offset.bytes().try_into().expect("too-large pointer offset");
let n = n as usize;
String::from_utf8(alloc.bytes[offset..(offset + n)].to_owned()).ok().map(Constant::Str)
},

View file

@ -53,7 +53,7 @@ impl LintPass for UnportableVariant {
}
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnportableVariant {
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss)]
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
if cx.tcx.data_layout.pointer_size.bits() != 64 {
return;

View file

@ -21,6 +21,7 @@
#![feature(tool_lints)]
#![warn(rust_2018_idioms, trivial_casts, trivial_numeric_casts)]
#![feature(crate_visibility_modifier)]
#![feature(try_from)]
// FIXME: switch to something more ergonomic here, once available.
// (currently there is no way to opt into sysroot crates w/o `extern crate`)

View file

@ -18,6 +18,7 @@ use crate::syntax::ast::{LitKind, NodeId, StrStyle};
use crate::syntax::source_map::{BytePos, Span};
use crate::utils::{is_expn_of, match_def_path, match_type, opt_def_id, paths, span_help_and_lint, span_lint};
use crate::consts::{constant, Constant};
use std::convert::TryFrom;
/// **What it does:** Checks [regex](https://crates.io/crates/regex) creation
/// (with `Regex::new`,`RegexBuilder::new` or `RegexSet::new`) for correct
@ -141,10 +142,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
}
}
#[allow(clippy::cast_possible_truncation)] // truncation very unlikely here
fn str_span(base: Span, c: regex_syntax::ast::Span, offset: u16) -> Span {
let offset = u32::from(offset);
let end = base.lo() + BytePos(c.end.offset as u32 + offset);
let start = base.lo() + BytePos(c.start.offset as u32 + offset);
let end = base.lo() + BytePos(u32::try_from(c.end.offset).expect("offset too large") + offset);
let start = base.lo() + BytePos(u32::try_from(c.start.offset).expect("offset too large") + offset);
assert!(start <= end);
Span::new(start, end, base.ctxt())
}

View file

@ -971,12 +971,14 @@ pub fn int_bits(tcx: TyCtxt<'_, '_, '_>, ity: ast::IntTy) -> u64 {
layout::Integer::from_attr(tcx, attr::IntType::SignedInt(ity)).size().bits()
}
#[allow(clippy::cast_possible_wrap)]
/// Turn a constant int byte representation into an i128
pub fn sext(tcx: TyCtxt<'_, '_, '_>, u: u128, ity: ast::IntTy) -> i128 {
let amt = 128 - int_bits(tcx, ity);
((u as i128) << amt) >> amt
}
#[allow(clippy::cast_sign_loss)]
/// clip unused bytes
pub fn unsext(tcx: TyCtxt<'_, '_, '_>, u: i128, ity: ast::IntTy) -> u128 {
let amt = 128 - int_bits(tcx, ity);

View file

@ -16,6 +16,7 @@ use crate::rustc::hir;
use crate::rustc::lint::{EarlyContext, LateContext, LintContext};
use crate::rustc_errors;
use std::borrow::Cow;
use std::convert::TryInto;
use std::fmt::Display;
use std;
use crate::syntax::source_map::{CharPos, Span};
@ -551,7 +552,7 @@ impl<'a, 'b, 'c, T: LintContext<'c>> DiagnosticBuilderExt<'c, T> for rustc_error
let non_whitespace_offset = src[fmpos.pos.to_usize()..].find(|c| c != ' ' && c != '\t' && c != '\n');
if let Some(non_whitespace_offset) = non_whitespace_offset {
remove_span = remove_span.with_hi(remove_span.hi() + BytePos(non_whitespace_offset as u32))
remove_span = remove_span.with_hi(remove_span.hi() + BytePos(non_whitespace_offset.try_into().expect("offset too large")))
}
}

View file

@ -12,6 +12,7 @@
#![feature(box_syntax)]
#![feature(rustc_private)]
#![feature(tool_lints)]
#![feature(try_from)]
#![allow(unknown_lints, clippy::missing_docs_in_private_items)]
// FIXME: switch to something more ergonomic here, once available.
@ -22,6 +23,7 @@ extern crate rustc_driver;
extern crate rustc_plugin;
use self::rustc_driver::{driver::CompileController, Compilation};
use std::convert::TryInto;
use std::path::Path;
use std::process::{exit, Command};
@ -153,5 +155,5 @@ pub fn main() {
let args = args;
rustc_driver::run_compiler(&args, Box::new(controller), None, None)
}) as i32)
}).try_into().expect("exit code too large"))
}