Skip to content

Commit eef2e89

Browse files
Fix cast_possible_truncation warnings
1 parent 995a974 commit eef2e89

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

clippy_lints/src/consts.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::rustc::ty::{self, Ty, TyCtxt, Instance};
1818
use crate::rustc::ty::subst::{Subst, Substs};
1919
use std::cmp::Ordering::{self, Equal};
2020
use std::cmp::PartialOrd;
21+
use std::convert::TryInto;
2122
use std::hash::{Hash, Hasher};
2223
use std::mem;
2324
use std::rc::Rc;
@@ -341,8 +342,12 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
341342
BinOpKind::Mul => l.checked_mul(r).map(zext),
342343
BinOpKind::Div if r != 0 => l.checked_div(r).map(zext),
343344
BinOpKind::Rem if r != 0 => l.checked_rem(r).map(zext),
344-
BinOpKind::Shr => l.checked_shr(r as u128 as u32).map(zext),
345-
BinOpKind::Shl => l.checked_shl(r as u128 as u32).map(zext),
345+
BinOpKind::Shr => l.checked_shr(
346+
(r as u128).try_into().expect("shift too large")
347+
).map(zext),
348+
BinOpKind::Shl => l.checked_shl(
349+
(r as u128).try_into().expect("shift too large")
350+
).map(zext),
346351
BinOpKind::BitXor => Some(zext(l ^ r)),
347352
BinOpKind::BitOr => Some(zext(l | r)),
348353
BinOpKind::BitAnd => Some(zext(l & r)),
@@ -362,8 +367,12 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
362367
BinOpKind::Mul => l.checked_mul(r).map(Constant::Int),
363368
BinOpKind::Div => l.checked_div(r).map(Constant::Int),
364369
BinOpKind::Rem => l.checked_rem(r).map(Constant::Int),
365-
BinOpKind::Shr => l.checked_shr(r as u32).map(Constant::Int),
366-
BinOpKind::Shl => l.checked_shl(r as u32).map(Constant::Int),
370+
BinOpKind::Shr => l.checked_shr(
371+
r.try_into().expect("shift too large")
372+
).map(Constant::Int),
373+
BinOpKind::Shl => l.checked_shl(
374+
r.try_into().expect("shift too large")
375+
).map(Constant::Int),
367376
BinOpKind::BitXor => Some(Constant::Int(l ^ r)),
368377
BinOpKind::BitOr => Some(Constant::Int(l | r)),
369378
BinOpKind::BitAnd => Some(Constant::Int(l & r)),
@@ -426,8 +435,12 @@ pub fn miri_to_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, result: &ty::Const<'
426435
ConstValue::Scalar(Scalar::Bits{ bits: b, ..}) => match result.ty.sty {
427436
ty::Bool => Some(Constant::Bool(b == 1)),
428437
ty::Uint(_) | ty::Int(_) => Some(Constant::Int(b)),
429-
ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(b as u32))),
430-
ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(b as u64))),
438+
ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(
439+
b.try_into().expect("invalid f32 bit representation")
440+
))),
441+
ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(
442+
b.try_into().expect("invalid f64 bit representation")
443+
))),
431444
// FIXME: implement other conversion
432445
_ => None,
433446
},
@@ -439,7 +452,7 @@ pub fn miri_to_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, result: &ty::Const<'
439452
.alloc_map
440453
.lock()
441454
.unwrap_memory(ptr.alloc_id);
442-
let offset = ptr.offset.bytes() as usize;
455+
let offset = ptr.offset.bytes().try_into().expect("too-large pointer offset");
443456
let n = n as usize;
444457
String::from_utf8(alloc.bytes[offset..(offset + n)].to_owned()).ok().map(Constant::Str)
445458
},

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#![feature(tool_lints)]
2222
#![warn(rust_2018_idioms, trivial_casts, trivial_numeric_casts)]
2323
#![feature(crate_visibility_modifier)]
24+
#![feature(try_from)]
2425

2526
// FIXME: switch to something more ergonomic here, once available.
2627
// (currently there is no way to opt into sysroot crates w/o `extern crate`)

clippy_lints/src/regex.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::syntax::ast::{LitKind, NodeId, StrStyle};
1818
use crate::syntax::source_map::{BytePos, Span};
1919
use crate::utils::{is_expn_of, match_def_path, match_type, opt_def_id, paths, span_help_and_lint, span_lint};
2020
use crate::consts::{constant, Constant};
21+
use std::convert::TryInto;
2122

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

144145
fn str_span(base: Span, c: regex_syntax::ast::Span, offset: u16) -> Span {
145146
let offset = u32::from(offset);
146-
let end = base.lo() + BytePos(c.end.offset as u32 + offset);
147-
let start = base.lo() + BytePos(c.start.offset as u32 + offset);
147+
let end = base.lo() + BytePos(
148+
c.end
149+
.offset
150+
.try_into()
151+
.ok()
152+
.and_then(|o: u32| o.checked_add(offset))
153+
.expect("offset too large"),
154+
);
155+
let start = base.lo() + BytePos(
156+
c.start
157+
.offset
158+
.try_into()
159+
.ok()
160+
.and_then(|o: u32| o.checked_add(offset))
161+
.expect("offset too large"),
162+
);
148163
assert!(start <= end);
149164
Span::new(start, end, base.ctxt())
150165
}

clippy_lints/src/utils/sugg.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::rustc::hir;
1616
use crate::rustc::lint::{EarlyContext, LateContext, LintContext};
1717
use crate::rustc_errors;
1818
use std::borrow::Cow;
19+
use std::convert::TryInto;
1920
use std::fmt::Display;
2021
use std;
2122
use crate::syntax::source_map::{CharPos, Span};
@@ -551,7 +552,7 @@ impl<'a, 'b, 'c, T: LintContext<'c>> DiagnosticBuilderExt<'c, T> for rustc_error
551552
let non_whitespace_offset = src[fmpos.pos.to_usize()..].find(|c| c != ' ' && c != '\t' && c != '\n');
552553

553554
if let Some(non_whitespace_offset) = non_whitespace_offset {
554-
remove_span = remove_span.with_hi(remove_span.hi() + BytePos(non_whitespace_offset as u32))
555+
remove_span = remove_span.with_hi(remove_span.hi() + BytePos(non_whitespace_offset.try_into().expect("offset too large")))
555556
}
556557
}
557558

src/driver.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![feature(box_syntax)]
1313
#![feature(rustc_private)]
1414
#![feature(tool_lints)]
15+
#![feature(try_from)]
1516
#![allow(unknown_lints, clippy::missing_docs_in_private_items)]
1617

1718
// FIXME: switch to something more ergonomic here, once available.
@@ -22,6 +23,7 @@ extern crate rustc_driver;
2223
extern crate rustc_plugin;
2324
use self::rustc_driver::{driver::CompileController, Compilation};
2425

26+
use std::convert::TryInto;
2527
use std::path::Path;
2628
use std::process::{exit, Command};
2729

@@ -153,5 +155,5 @@ pub fn main() {
153155

154156
let args = args;
155157
rustc_driver::run_compiler(&args, Box::new(controller), None, None)
156-
}) as i32)
158+
}).try_into().expect("exit code too large"))
157159
}

0 commit comments

Comments
 (0)