Skip to content

Commit 8127440

Browse files
authored
Merge pull request #391 from bjorn3/clippy_fixes
Clippy fixes
2 parents 9143a69 + 52bf473 commit 8127440

File tree

8 files changed

+52
-60
lines changed

8 files changed

+52
-60
lines changed

src/fn_call.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
199199
self.write_null(dest, dest_ty)?;
200200
} else {
201201
let align = self.tcx.data_layout.pointer_align;
202-
let ptr = self.memory.allocate(Size::from_bytes(size), align, Some(MemoryKind::C.into()))?;
202+
let ptr = self.memory.allocate(Size::from_bytes(size), align, MemoryKind::C.into())?;
203203
self.write_scalar(dest, Scalar::Ptr(ptr), dest_ty)?;
204204
}
205205
}
@@ -268,7 +268,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
268268
)?;
269269
let mut args = self.frame().mir.args_iter();
270270

271-
let arg_local = args.next().ok_or(
271+
let arg_local = args.next().ok_or_else(||
272272
EvalErrorKind::AbiViolation(
273273
"Argument to __rust_maybe_catch_panic does not take enough arguments."
274274
.to_owned(),
@@ -395,7 +395,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
395395
let value_copy = self.memory.allocate(
396396
Size::from_bytes((value.len() + 1) as u64),
397397
Align::from_bytes(1, 1).unwrap(),
398-
Some(MemoryKind::Env.into()),
398+
MemoryKind::Env.into(),
399399
)?;
400400
self.memory.write_bytes(value_copy.into(), &value)?;
401401
let trailing_zero_ptr = value_copy.offset(Size::from_bytes(value.len() as u64), &self)?.into();
@@ -504,7 +504,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
504504

505505
// Figure out how large a pthread TLS key actually is. This is libc::pthread_key_t.
506506
let key_type = args[0].ty.builtin_deref(true)
507-
.ok_or(EvalErrorKind::AbiViolation("Wrong signature used for pthread_key_create: First argument must be a raw pointer.".to_owned()))?.ty;
507+
.ok_or_else(|| EvalErrorKind::AbiViolation("Wrong signature used for pthread_key_create: First argument must be a raw pointer.".to_owned()))?.ty;
508508
let key_size = self.layout_of(key_type)?.size;
509509

510510
// Create key and write it into the memory where key_ptr wants it
@@ -656,7 +656,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
656656
}
657657
let ptr = self.memory.allocate(Size::from_bytes(size),
658658
Align::from_bytes(align, align).unwrap(),
659-
Some(MemoryKind::Rust.into()))?;
659+
MemoryKind::Rust.into())?;
660660
self.write_scalar(dest, Scalar::Ptr(ptr), dest_ty)?;
661661
}
662662
"alloc::alloc::::__rust_alloc_zeroed" => {
@@ -670,7 +670,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
670670
}
671671
let ptr = self.memory.allocate(Size::from_bytes(size),
672672
Align::from_bytes(align, align).unwrap(),
673-
Some(MemoryKind::Rust.into()))?;
673+
MemoryKind::Rust.into())?;
674674
self.memory.write_repeat(ptr.into(), 0, Size::from_bytes(size))?;
675675
self.write_scalar(dest, Scalar::Ptr(ptr), dest_ty)?;
676676
}
@@ -747,7 +747,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
747747
// current frame.
748748
self.dump_local(dest);
749749
self.goto_block(dest_block);
750-
return Ok(());
750+
Ok(())
751751
}
752752

753753
fn write_null(&mut self, dest: Place, dest_ty: Ty<'tcx>) -> EvalResult<'tcx> {

src/helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super:
7575
}
7676
// FIXME: assuming here that type size is < i64::max_value()
7777
let pointee_size = self.layout_of(pointee_ty)?.size.bytes() as i64;
78-
return if let Some(offset) = offset.checked_mul(pointee_size) {
78+
if let Some(offset) = offset.checked_mul(pointee_size) {
7979
let ptr = ptr.ptr_signed_offset(offset, self)?;
8080
// Do not do bounds-checking for integers; they can never alias a normal pointer anyway.
8181
if let Scalar::Ptr(ptr) = ptr {
@@ -87,7 +87,7 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super:
8787
Ok(ptr)
8888
} else {
8989
err!(Overflow(mir::BinOp::Mul))
90-
};
90+
}
9191
}
9292

9393
fn value_to_isize(

src/lib.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
inclusive_range_methods,
66
)]
77

8+
#![cfg_attr(feature = "cargo-clippy", allow(cast_lossless))]
9+
810
#[macro_use]
911
extern crate log;
1012

@@ -24,7 +26,6 @@ use rustc::ty::layout::{TyLayout, LayoutOf, Size};
2426
use rustc::ty::subst::Subst;
2527
use rustc::hir::def_id::DefId;
2628
use rustc::mir;
27-
use rustc::middle::const_val;
2829

2930
use syntax::ast::Mutability;
3031
use syntax::codemap::Span;
@@ -172,7 +173,7 @@ pub fn create_ecx<'a, 'mir: 'a, 'tcx: 'mir>(
172173
// Return value
173174
let size = ecx.tcx.data_layout.pointer_size;
174175
let align = ecx.tcx.data_layout.pointer_align;
175-
let ret_ptr = ecx.memory_mut().allocate(size, align, Some(MemoryKind::Stack))?;
176+
let ret_ptr = ecx.memory_mut().allocate(size, align, MemoryKind::Stack)?;
176177
cleanup_ptr = Some(ret_ptr);
177178

178179
// Push our stack frame
@@ -211,7 +212,7 @@ pub fn create_ecx<'a, 'mir: 'a, 'tcx: 'mir>(
211212
let foo = ecx.memory.allocate_bytes(b"foo\0");
212213
let ptr_size = ecx.memory.pointer_size();
213214
let ptr_align = ecx.tcx.data_layout.pointer_align;
214-
let foo_ptr = ecx.memory.allocate(ptr_size, ptr_align, None)?;
215+
let foo_ptr = ecx.memory.allocate(ptr_size, ptr_align, MemoryKind::Stack)?;
215216
ecx.memory.write_scalar(foo_ptr.into(), ptr_align, Scalar::Ptr(foo), ptr_size, false)?;
216217
ecx.memory.mark_static_initialized(foo_ptr.alloc_id, Mutability::Immutable)?;
217218
ecx.write_ptr(dest, foo_ptr.into(), ty)?;
@@ -270,10 +271,10 @@ pub fn eval_main<'a, 'tcx: 'a>(
270271
block.terminator().source_info.span
271272
};
272273

273-
let mut err = const_val::struct_error(ecx.tcx.tcx.at(span), "constant evaluation error");
274+
let mut err = struct_error(ecx.tcx.tcx.at(span), "constant evaluation error");
274275
let (frames, span) = ecx.generate_stacktrace(None);
275276
err.span_label(span, e.to_string());
276-
for const_val::FrameInfo { span, location, .. } in frames {
277+
for FrameInfo { span, location, .. } in frames {
277278
err.span_note(span, &format!("inside call to `{}`", location));
278279
}
279280
err.emit();
@@ -405,7 +406,7 @@ impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> {
405406
let ptr = ecx.memory.allocate(
406407
layout.size,
407408
layout.align,
408-
None,
409+
MemoryKind::Stack,
409410
)?;
410411

411412
// Step 4: Cache allocation id for recursive statics
@@ -428,14 +429,11 @@ impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> {
428429
let frame = ecx.frame_mut();
429430
let bb = &frame.mir.basic_blocks()[frame.block];
430431
if bb.statements.len() == frame.stmt && !bb.is_cleanup {
431-
match bb.terminator().kind {
432-
::rustc::mir::TerminatorKind::Return => {
433-
for (local, _local_decl) in mir.local_decls.iter_enumerated().skip(1) {
434-
// Don't deallocate locals, because the return value might reference them
435-
frame.storage_dead(local);
436-
}
432+
if let ::rustc::mir::TerminatorKind::Return = bb.terminator().kind {
433+
for (local, _local_decl) in mir.local_decls.iter_enumerated().skip(1) {
434+
// Don't deallocate locals, because the return value might reference them
435+
frame.storage_dead(local);
437436
}
438-
_ => {}
439437
}
440438
}
441439
}
@@ -479,7 +477,7 @@ impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> {
479477
value: Value::Scalar(Scalar::from_u128(match layout.size.bytes() {
480478
0 => 1 as u128,
481479
size => size as u128,
482-
}.into())),
480+
})),
483481
ty: usize,
484482
},
485483
dest,

src/locks.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,9 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> MemoryExt<'tcx> for Memory<'a, 'mir, 'tcx, Evalu
241241
// All is well
242242
continue 'locks;
243243
}
244-
} else {
245-
if !is_our_lock {
246-
// All is well.
247-
continue 'locks;
248-
}
244+
} else if !is_our_lock {
245+
// All is well.
246+
continue 'locks;
249247
}
250248
// If we get here, releasing this is an error except for NoLock.
251249
if lock.active != NoLock {
@@ -377,7 +375,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> MemoryExt<'tcx> for Memory<'a, 'mir, 'tcx, Evalu
377375
}
378376
// Clean up the map
379377
alloc_locks.retain(|lock| match lock.active {
380-
NoLock => lock.suspended.len() > 0,
378+
NoLock => !lock.suspended.is_empty(),
381379
_ => true,
382380
});
383381
}

src/operator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super:
6969
.expect("Offset called on non-ptr type")
7070
.ty;
7171
let ptr = self.pointer_offset(
72-
left.into(),
72+
left,
7373
pointee_ty,
7474
right.to_bits(self.memory.pointer_size())? as i64,
7575
)?;

src/range_map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct RangeMap<T> {
1919
// At the same time the `end` is irrelevant for the sorting and range searching, but used for the check.
2020
// This kind of search breaks, if `end < start`, so don't do that!
2121
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
22-
struct Range {
22+
pub struct Range {
2323
start: u64,
2424
end: u64, // Invariant: end > start
2525
}
@@ -189,7 +189,7 @@ impl<T> RangeMap<T> {
189189
F: FnMut(&T) -> bool,
190190
{
191191
let mut remove = Vec::new();
192-
for (range, data) in self.map.iter() {
192+
for (range, data) in &self.map {
193193
if !f(data) {
194194
remove.push(*range);
195195
}

src/tls.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,38 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> MemoryExt<'tcx> for Memory<'a, 'mir, 'tcx, Evalu
3030
},
3131
);
3232
trace!("New TLS key allocated: {} with dtor {:?}", new_key, dtor);
33-
return new_key;
33+
new_key
3434
}
3535

3636
fn delete_tls_key(&mut self, key: TlsKey) -> EvalResult<'tcx> {
37-
return match self.data.thread_local.remove(&key) {
37+
match self.data.thread_local.remove(&key) {
3838
Some(_) => {
3939
trace!("TLS key {} removed", key);
4040
Ok(())
4141
}
4242
None => err!(TlsOutOfBounds),
43-
};
43+
}
4444
}
4545

4646
fn load_tls(&mut self, key: TlsKey) -> EvalResult<'tcx, Scalar> {
47-
return match self.data.thread_local.get(&key) {
47+
match self.data.thread_local.get(&key) {
4848
Some(&TlsEntry { data, .. }) => {
4949
trace!("TLS key {} loaded: {:?}", key, data);
5050
Ok(data)
5151
}
5252
None => err!(TlsOutOfBounds),
53-
};
53+
}
5454
}
5555

5656
fn store_tls(&mut self, key: TlsKey, new_data: Scalar) -> EvalResult<'tcx> {
57-
return match self.data.thread_local.get_mut(&key) {
57+
match self.data.thread_local.get_mut(&key) {
5858
Some(&mut TlsEntry { ref mut data, .. }) => {
5959
trace!("TLS key {} stored: {:?}", key, new_data);
6060
*data = new_data;
6161
Ok(())
6262
}
6363
None => err!(TlsOutOfBounds),
64-
};
64+
}
6565
}
6666

6767
/// Returns a dtor, its argument and its index, if one is supposed to run
@@ -104,7 +104,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> MemoryExt<'tcx> for Memory<'a, 'mir, 'tcx, Evalu
104104
}
105105
}
106106
}
107-
return Ok(None);
107+
Ok(None)
108108
}
109109
}
110110

@@ -124,8 +124,8 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
124124
Place::undef(),
125125
StackPopCleanup::None,
126126
)?;
127-
let arg_local = self.frame().mir.args_iter().next().ok_or(
128-
EvalErrorKind::AbiViolation("TLS dtor does not take enough arguments.".to_owned()),
127+
let arg_local = self.frame().mir.args_iter().next().ok_or_else(
128+
|| EvalErrorKind::AbiViolation("TLS dtor does not take enough arguments.".to_owned()),
129129
)?;
130130
let dest = self.eval_place(&mir::Place::Local(arg_local))?;
131131
let ty = self.tcx.mk_mut_ptr(self.tcx.types.u8);

src/validation.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc::ty::subst::{Substs, Subst};
88
use rustc::traits::{self, TraitEngine};
99
use rustc::infer::InferCtxt;
1010
use rustc::middle::region;
11-
use rustc::middle::const_val::ConstVal;
11+
use rustc::mir::interpret::{ConstValue};
1212
use rustc_data_structures::indexed_vec::Idx;
1313
use rustc_mir::interpret::HasMemory;
1414

@@ -135,10 +135,10 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
135135
}
136136

137137
fn abstract_place(&self, place: &mir::Place<'tcx>) -> EvalResult<'tcx, AbsPlace<'tcx>> {
138-
Ok(match place {
139-
&mir::Place::Local(l) => AbsPlace::Local(l),
140-
&mir::Place::Static(ref s) => AbsPlace::Static(s.def_id),
141-
&mir::Place::Projection(ref p) =>
138+
Ok(match *place {
139+
mir::Place::Local(l) => AbsPlace::Local(l),
140+
mir::Place::Static(ref s) => AbsPlace::Static(s.def_id),
141+
mir::Place::Projection(ref p) =>
142142
AbsPlace::Projection(Box::new(self.abstract_place_projection(&*p)?)),
143143
})
144144
}
@@ -378,11 +378,8 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
378378
mut layout: ty::layout::TyLayout<'tcx>,
379379
i: usize,
380380
) -> EvalResult<'tcx, Ty<'tcx>> {
381-
match base {
382-
Place::Ptr { extra: PlaceExtra::DowncastVariant(variant_index), .. } => {
383-
layout = layout.for_variant(&self, variant_index);
384-
}
385-
_ => {}
381+
if let Place::Ptr { extra: PlaceExtra::DowncastVariant(variant_index), .. } = base {
382+
layout = layout.for_variant(&self, variant_index);
386383
}
387384
let tcx = self.tcx.tcx;
388385
Ok(match layout.ty.sty {
@@ -667,12 +664,11 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
667664
// Inner lifetimes *outlive* outer ones, so only if we have no lifetime restriction yet,
668665
// we record the region of this borrow to the context.
669666
if query.re == None {
670-
match *region {
671-
ReScope(scope) => query.re = Some(scope),
672-
// It is possible for us to encounter erased lifetimes here because the lifetimes in
673-
// this functions' Subst will be erased.
674-
_ => {}
667+
if let ReScope(scope) = *region {
668+
query.re = Some(scope);
675669
}
670+
// It is possible for us to encounter erased lifetimes here because the lifetimes in
671+
// this functions' Subst will be erased.
676672
}
677673
self.validate_ptr(val, query.place.0, pointee_ty, query.re, query.mutbl, mode)?;
678674
}
@@ -719,14 +715,14 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
719715
}
720716
TyArray(elem_ty, len) => {
721717
let len = match len.val {
722-
ConstVal::Unevaluated(def_id, substs) => {
718+
ConstValue::Unevaluated(def_id, substs) => {
723719
self.tcx.const_eval(self.tcx.param_env(def_id).and(GlobalId {
724720
instance: Instance::new(def_id, substs),
725721
promoted: None,
726722
}))
727723
.map_err(|_err|EvalErrorKind::MachineError("<already reported>".to_string()))?
728724
}
729-
ConstVal::Value(_) => len,
725+
_ => len,
730726
};
731727
let len = len.unwrap_usize(self.tcx.tcx);
732728
for i in 0..len {
@@ -772,7 +768,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
772768
let variant_idx = self.read_discriminant_as_variant_index(query.place.1, query.ty)?;
773769
let variant = &adt.variants[variant_idx];
774770

775-
if variant.fields.len() > 0 {
771+
if !variant.fields.is_empty() {
776772
// Downcast to this variant, if needed
777773
let place = if adt.is_enum() {
778774
(

0 commit comments

Comments
 (0)