Skip to content

Commit

Permalink
Fix cast_possible_truncation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
devonhollowood committed Oct 9, 2018
1 parent 995a974 commit eef2e89
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
27 changes: 20 additions & 7 deletions clippy_lints/src/consts.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -341,8 +342,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 as u128).try_into().expect("shift too large")
).map(zext),
BinOpKind::Shl => l.checked_shl(
(r as u128).try_into().expect("shift too large")
).map(zext),
BinOpKind::BitXor => Some(zext(l ^ r)),
BinOpKind::BitOr => Some(zext(l | r)),
BinOpKind::BitAnd => Some(zext(l & r)),
Expand All @@ -362,8 +367,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)),
Expand Down Expand Up @@ -426,8 +435,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,
},
Expand All @@ -439,7 +452,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)
},
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.rs
Expand Up @@ -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`)
Expand Down
19 changes: 17 additions & 2 deletions clippy_lints/src/regex.rs
Expand Up @@ -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::TryInto;

/// **What it does:** Checks [regex](https://crates.io/crates/regex) creation
/// (with `Regex::new`,`RegexBuilder::new` or `RegexSet::new`) for correct
Expand Down Expand Up @@ -143,8 +144,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {

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(
c.end
.offset
.try_into()
.ok()
.and_then(|o: u32| o.checked_add(offset))
.expect("offset too large"),
);
let start = base.lo() + BytePos(
c.start
.offset
.try_into()
.ok()
.and_then(|o: u32| o.checked_add(offset))
.expect("offset too large"),
);
assert!(start <= end);
Span::new(start, end, base.ctxt())
}
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/utils/sugg.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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")))
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/driver.rs
Expand Up @@ -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.
Expand All @@ -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};

Expand Down Expand Up @@ -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"))
}

0 comments on commit eef2e89

Please sign in to comment.