Skip to content

Commit 928a6d3

Browse files
authored
Merge pull request #3288 from devonhollowood/pedantic-dogfood-casts
Pedantic dogfood: casts
2 parents 31eb3b7 + 289c642 commit 928a6d3

File tree

7 files changed

+34
-12
lines changed

7 files changed

+34
-12
lines changed

clippy_lints/src/consts.rs

Lines changed: 21 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;
@@ -229,6 +230,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
229230
}
230231
}
231232

233+
#[allow(clippy::cast_possible_wrap)]
232234
fn constant_not(&self, o: &Constant, ty: ty::Ty<'_>) -> Option<Constant> {
233235
use self::Constant::*;
234236
match *o {
@@ -341,8 +343,12 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
341343
BinOpKind::Mul => l.checked_mul(r).map(zext),
342344
BinOpKind::Div if r != 0 => l.checked_div(r).map(zext),
343345
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),
346+
BinOpKind::Shr => l.checked_shr(
347+
r.try_into().expect("invalid shift")
348+
).map(zext),
349+
BinOpKind::Shl => l.checked_shl(
350+
r.try_into().expect("invalid shift")
351+
).map(zext),
346352
BinOpKind::BitXor => Some(zext(l ^ r)),
347353
BinOpKind::BitOr => Some(zext(l | r)),
348354
BinOpKind::BitAnd => Some(zext(l & r)),
@@ -362,8 +368,12 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
362368
BinOpKind::Mul => l.checked_mul(r).map(Constant::Int),
363369
BinOpKind::Div => l.checked_div(r).map(Constant::Int),
364370
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),
371+
BinOpKind::Shr => l.checked_shr(
372+
r.try_into().expect("shift too large")
373+
).map(Constant::Int),
374+
BinOpKind::Shl => l.checked_shl(
375+
r.try_into().expect("shift too large")
376+
).map(Constant::Int),
367377
BinOpKind::BitXor => Some(Constant::Int(l ^ r)),
368378
BinOpKind::BitOr => Some(Constant::Int(l | r)),
369379
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<'
426436
ConstValue::Scalar(Scalar::Bits{ bits: b, ..}) => match result.ty.sty {
427437
ty::Bool => Some(Constant::Bool(b == 1)),
428438
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))),
439+
ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(
440+
b.try_into().expect("invalid f32 bit representation")
441+
))),
442+
ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(
443+
b.try_into().expect("invalid f64 bit representation")
444+
))),
431445
// FIXME: implement other conversion
432446
_ => None,
433447
},
@@ -439,7 +453,7 @@ pub fn miri_to_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, result: &ty::Const<'
439453
.alloc_map
440454
.lock()
441455
.unwrap_memory(ptr.alloc_id);
442-
let offset = ptr.offset.bytes() as usize;
456+
let offset = ptr.offset.bytes().try_into().expect("too-large pointer offset");
443457
let n = n as usize;
444458
String::from_utf8(alloc.bytes[offset..(offset + n)].to_owned()).ok().map(Constant::Str)
445459
},

clippy_lints/src/enum_clike.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl LintPass for UnportableVariant {
5353
}
5454

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

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: 4 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::TryFrom;
2122

2223
/// **What it does:** Checks [regex](https://crates.io/crates/regex) creation
2324
/// (with `Regex::new`,`RegexBuilder::new` or `RegexSet::new`) for correct
@@ -141,10 +142,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
141142
}
142143
}
143144

145+
#[allow(clippy::cast_possible_truncation)] // truncation very unlikely here
144146
fn str_span(base: Span, c: regex_syntax::ast::Span, offset: u16) -> Span {
145147
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);
148+
let end = base.lo() + BytePos(u32::try_from(c.end.offset).expect("offset too large") + offset);
149+
let start = base.lo() + BytePos(u32::try_from(c.start.offset).expect("offset too large") + offset);
148150
assert!(start <= end);
149151
Span::new(start, end, base.ctxt())
150152
}

clippy_lints/src/utils/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,12 +971,14 @@ pub fn int_bits(tcx: TyCtxt<'_, '_, '_>, ity: ast::IntTy) -> u64 {
971971
layout::Integer::from_attr(tcx, attr::IntType::SignedInt(ity)).size().bits()
972972
}
973973

974+
#[allow(clippy::cast_possible_wrap)]
974975
/// Turn a constant int byte representation into an i128
975976
pub fn sext(tcx: TyCtxt<'_, '_, '_>, u: u128, ity: ast::IntTy) -> i128 {
976977
let amt = 128 - int_bits(tcx, ity);
977978
((u as i128) << amt) >> amt
978979
}
979980

981+
#[allow(clippy::cast_sign_loss)]
980982
/// clip unused bytes
981983
pub fn unsext(tcx: TyCtxt<'_, '_, '_>, u: i128, ity: ast::IntTy) -> u128 {
982984
let amt = 128 - int_bits(tcx, ity);

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)