Fix clippy warnings.

This commit fixes the current set of (stable) clippy warnings in the repo.
This commit is contained in:
Peter Huene
2019-10-23 23:15:42 -07:00
committed by Andrew Brown
parent 1176e4f178
commit 9f506692c2
93 changed files with 667 additions and 662 deletions

View File

@@ -168,10 +168,12 @@ pub(crate) enum Literal {
impl Literal {
pub fn enumerator_for(kind: &OperandKind, value: &'static str) -> Self {
let value = match &kind.fields {
OperandKindFields::ImmEnum(values) => values.get(value).expect(&format!(
"nonexistent value '{}' in enumeration '{}'",
value, kind.name
)),
OperandKindFields::ImmEnum(values) => values.get(value).unwrap_or_else(|| {
panic!(
"nonexistent value '{}' in enumeration '{}'",
value, kind.name
)
}),
_ => panic!("enumerator is for enum values"),
};
Literal::Enumerator {

View File

@@ -53,7 +53,7 @@ impl CpuMode {
Some(typ) => self
.typed_legalize
.get(typ)
.map(|x| *x)
.copied()
.unwrap_or_else(|| self.get_default_legalize_code()),
None => self
.monomorphic_legalize

View File

@@ -61,8 +61,8 @@ impl InstructionGroup {
pub fn by_name(&self, name: &'static str) -> &Instruction {
self.instructions
.iter()
.find(|inst| &inst.name == name)
.expect(&format!("unexisting instruction with name {}", name))
.find(|inst| inst.name == name)
.unwrap_or_else(|| panic!("unexisting instruction with name {}", name))
}
}
@@ -167,7 +167,7 @@ impl Bindable for Instruction {
impl fmt::Display for InstructionContent {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
if self.operands_out.len() > 0 {
if !self.operands_out.is_empty() {
let operands_out = self
.operands_out
.iter()
@@ -180,7 +180,7 @@ impl fmt::Display for InstructionContent {
fmt.write_str(&self.name)?;
if self.operands_in.len() > 0 {
if !self.operands_in.is_empty() {
let operands_in = self
.operands_in
.iter()
@@ -244,53 +244,70 @@ impl InstructionBuilder {
self.operands_in = Some(operands.iter().map(|x| (*x).clone()).collect());
self
}
pub fn operands_out(mut self, operands: Vec<&Operand>) -> Self {
assert!(self.operands_out.is_none());
self.operands_out = Some(operands.iter().map(|x| (*x).clone()).collect());
self
}
pub fn constraints(mut self, constraints: Vec<Constraint>) -> Self {
assert!(self.constraints.is_none());
self.constraints = Some(constraints);
self
}
#[allow(clippy::wrong_self_convention)]
pub fn is_terminator(mut self, val: bool) -> Self {
self.is_terminator = val;
self
}
#[allow(clippy::wrong_self_convention)]
pub fn is_branch(mut self, val: bool) -> Self {
self.is_branch = val;
self
}
#[allow(clippy::wrong_self_convention)]
pub fn is_indirect_branch(mut self, val: bool) -> Self {
self.is_indirect_branch = val;
self
}
#[allow(clippy::wrong_self_convention)]
pub fn is_call(mut self, val: bool) -> Self {
self.is_call = val;
self
}
#[allow(clippy::wrong_self_convention)]
pub fn is_return(mut self, val: bool) -> Self {
self.is_return = val;
self
}
#[allow(clippy::wrong_self_convention)]
pub fn is_ghost(mut self, val: bool) -> Self {
self.is_ghost = val;
self
}
pub fn can_load(mut self, val: bool) -> Self {
self.can_load = val;
self
}
pub fn can_store(mut self, val: bool) -> Self {
self.can_store = val;
self
}
pub fn can_trap(mut self, val: bool) -> Self {
self.can_trap = val;
self
}
pub fn other_side_effects(mut self, val: bool) -> Self {
self.other_side_effects = val;
self
@@ -534,7 +551,7 @@ impl Bindable for BoundInstruction {
}
/// Checks that the input operands actually match the given format.
fn verify_format(inst_name: &str, operands_in: &Vec<Operand>, format: &InstructionFormat) {
fn verify_format(inst_name: &str, operands_in: &[Operand], format: &InstructionFormat) {
// A format is defined by:
// - its number of input value operands,
// - its number and names of input immediate operands,
@@ -586,10 +603,10 @@ fn verify_format(inst_name: &str, operands_in: &Vec<Operand>, format: &Instructi
/// Check if this instruction is polymorphic, and verify its use of type variables.
fn verify_polymorphic(
operands_in: &Vec<Operand>,
operands_out: &Vec<Operand>,
operands_in: &[Operand],
operands_out: &[Operand],
format: &InstructionFormat,
value_opnums: &Vec<usize>,
value_opnums: &[usize],
) -> Option<PolymorphicInfo> {
// The instruction is polymorphic if it has one free input or output operand.
let is_polymorphic = operands_in
@@ -633,7 +650,7 @@ fn verify_polymorphic(
// If we reached here, it means the type variable indicated as the typevar operand couldn't
// control every other input and output type variable. We need to look at the result type
// variables.
if operands_out.len() == 0 {
if operands_out.is_empty() {
// No result means no other possible type variable, so it's a type inference failure.
match maybe_error_message {
Some(msg) => panic!(msg),
@@ -670,8 +687,8 @@ fn verify_polymorphic(
/// Return a vector of other type variables used, or a string explaining what went wrong.
fn is_ctrl_typevar_candidate(
ctrl_typevar: &TypeVar,
operands_in: &Vec<Operand>,
operands_out: &Vec<Operand>,
operands_in: &[Operand],
operands_out: &[Operand],
) -> Result<Vec<TypeVar>, String> {
let mut other_typevars = Vec::new();
@@ -995,8 +1012,7 @@ impl InstructionPredicate {
.value_opnums
.iter()
.enumerate()
.filter(|(_, &op_num)| inst.operands_in[op_num].type_var().unwrap() == type_var)
.next()
.find(|(_, &op_num)| inst.operands_in[op_num].type_var().unwrap() == type_var)
.unwrap()
.0;
InstructionPredicateNode::TypePredicate(TypePredicateNode::TypeVarCheck(

View File

@@ -42,7 +42,7 @@ impl Stack {
pub fn new(regclass: RegClassIndex) -> Self {
Self { regclass }
}
pub fn stack_base_mask(&self) -> &'static str {
pub fn stack_base_mask(self) -> &'static str {
// TODO: Make this configurable instead of just using the SP.
"StackBaseMask(1)"
}
@@ -253,8 +253,8 @@ impl EncodingRecipeBuilder {
}
pub fn build(self) -> EncodingRecipe {
let operands_in = self.operands_in.unwrap_or(Vec::new());
let operands_out = self.operands_out.unwrap_or(Vec::new());
let operands_in = self.operands_in.unwrap_or_default();
let operands_out = self.operands_out.unwrap_or_default();
// The number of input constraints must match the number of format input operands.
if !self.format.has_value_list {
@@ -282,7 +282,7 @@ impl EncodingRecipeBuilder {
let clobbers_flags = self.clobbers_flags.unwrap_or(true);
EncodingRecipe {
name: self.name.into(),
name: self.name,
format: self.format,
base_size: self.base_size,
operands_in,

View File

@@ -164,7 +164,7 @@ impl RegClassBuilder {
name,
width: 0,
count: stop - start,
start: start,
start,
proto: RegClassProto::SubClass(parent_index),
}
}
@@ -214,7 +214,7 @@ impl RegBankBuilder {
self
}
pub fn pinned_reg(mut self, unit: u16) -> Self {
assert!(unit < (self.units as u16));
assert!(unit < u16::from(self.units));
self.pinned_reg = Some(unit);
self
}
@@ -234,7 +234,7 @@ impl IsaRegsBuilder {
}
pub fn add_bank(&mut self, builder: RegBankBuilder) -> RegBankIndex {
let first_unit = if self.banks.len() == 0 {
let first_unit = if self.banks.is_empty() {
0
} else {
let last = &self.banks.last().unwrap();
@@ -358,8 +358,7 @@ impl IsaRegsBuilder {
.unwrap()
.subclasses
.iter()
.find(|x| **x == *i2)
.is_some());
.any(|x| *x == *i2));
}
}
}
@@ -402,7 +401,7 @@ impl IsaRegs {
self.classes
.values()
.find(|&class| class.name == name)
.expect(&format!("register class {} not found", name))
.unwrap_or_else(|| panic!("register class {} not found", name))
.index
}

View File

@@ -268,7 +268,7 @@ impl SettingGroupBuilder {
default: bool,
) -> BoolSettingIndex {
assert!(
self.predicates.len() == 0,
self.predicates.is_empty(),
"predicates must be added after the boolean settings"
);
self.add_setting(name, comment, ProtoSpecificSetting::Bool(default));
@@ -379,7 +379,7 @@ impl SettingGroupBuilder {
}
assert!(
group.predicates.len() == 0,
group.predicates.is_empty(),
"settings_size is the byte size before adding predicates"
);
group.settings_size = group.byte_size();
@@ -393,11 +393,11 @@ impl SettingGroupBuilder {
.extend(predicates.into_iter().map(|predicate| {
let number = predicate_number;
predicate_number += 1;
return Predicate {
Predicate {
name: predicate.name,
node: predicate.node,
number,
};
}
}));
group.presets.extend(self.presets);

View File

@@ -74,7 +74,7 @@ impl Constraint {
}
// Trivially false.
if (&ts1.lanes & &ts2.lanes).len() == 0 {
if (&ts1.lanes & &ts2.lanes).is_empty() {
return true;
}
@@ -155,12 +155,7 @@ impl TypeEnvironment {
}
fn add_constraint(&mut self, constraint: Constraint) {
if self
.constraints
.iter()
.find(|&item| item == &constraint)
.is_some()
{
if self.constraints.iter().any(|item| *item == constraint) {
return;
}
@@ -257,7 +252,7 @@ impl TypeEnvironment {
.map(|tv| self.get_equivalent(tv).free_typevar())
.filter(|opt_tv| {
// Filter out singleton types.
return opt_tv.is_some();
opt_tv.is_some()
})
.map(|tv| tv.unwrap()),
);
@@ -306,7 +301,7 @@ impl TypeEnvironment {
children
.entry(parent_tv)
.or_insert(HashSet::new())
.or_insert_with(HashSet::new)
.insert(type_var.clone());
}
@@ -314,7 +309,7 @@ impl TypeEnvironment {
for (equivalent_tv, canon_tv) in self.equivalency_map.iter() {
children
.entry(canon_tv.clone())
.or_insert(HashSet::new())
.or_insert_with(HashSet::new)
.insert(equivalent_tv.clone());
}
@@ -604,7 +599,7 @@ fn infer_definition(
/// Perform type inference on an transformation. Return an updated type environment or error.
pub(crate) fn infer_transform(
src: DefIndex,
dst: &Vec<DefIndex>,
dst: &[DefIndex],
def_pool: &DefPool,
var_pool: &mut VarPool,
) -> Result<TypeEnvironment, String> {

View File

@@ -6,7 +6,7 @@ use crate::shared::types as shared_types;
use cranelift_codegen_shared::constants;
// Rust name prefix used for the `rust_name` method.
static _RUST_NAME_PREFIX: &'static str = "ir::types::";
static _RUST_NAME_PREFIX: &str = "ir::types::";
// ValueType variants (i8, i32, ...) are provided in `shared::types.rs`.
@@ -242,29 +242,29 @@ impl LaneType {
})
}
pub fn by(&self, lanes: u16) -> ValueType {
pub fn by(self, lanes: u16) -> ValueType {
if lanes == 1 {
(*self).into()
self.into()
} else {
ValueType::Vector(VectorType::new(*self, lanes.into()))
ValueType::Vector(VectorType::new(self, lanes.into()))
}
}
pub fn is_float(&self) -> bool {
pub fn is_float(self) -> bool {
match self {
LaneType::FloatType(_) => true,
_ => false,
}
}
pub fn is_int(&self) -> bool {
pub fn is_int(self) -> bool {
match self {
LaneType::IntType(_) => true,
_ => false,
}
}
pub fn is_bool(&self) -> bool {
pub fn is_bool(self) -> bool {
match self {
LaneType::BoolType(_) => true,
_ => false,

View File

@@ -156,33 +156,33 @@ impl TypeVar {
let ts = self.get_typeset();
// Safety checks to avoid over/underflows.
assert!(ts.specials.len() == 0, "can't derive from special types");
assert!(ts.specials.is_empty(), "can't derive from special types");
match derived_func {
DerivedFunc::HalfWidth => {
assert!(
ts.ints.len() == 0 || *ts.ints.iter().min().unwrap() > 8,
ts.ints.is_empty() || *ts.ints.iter().min().unwrap() > 8,
"can't halve all integer types"
);
assert!(
ts.floats.len() == 0 || *ts.floats.iter().min().unwrap() > 32,
ts.floats.is_empty() || *ts.floats.iter().min().unwrap() > 32,
"can't halve all float types"
);
assert!(
ts.bools.len() == 0 || *ts.bools.iter().min().unwrap() > 8,
ts.bools.is_empty() || *ts.bools.iter().min().unwrap() > 8,
"can't halve all boolean types"
);
}
DerivedFunc::DoubleWidth => {
assert!(
ts.ints.len() == 0 || *ts.ints.iter().max().unwrap() < MAX_BITS,
ts.ints.is_empty() || *ts.ints.iter().max().unwrap() < MAX_BITS,
"can't double all integer types"
);
assert!(
ts.floats.len() == 0 || *ts.floats.iter().max().unwrap() < MAX_FLOAT_BITS,
ts.floats.is_empty() || *ts.floats.iter().max().unwrap() < MAX_FLOAT_BITS,
"can't double all float types"
);
assert!(
ts.bools.len() == 0 || *ts.bools.iter().max().unwrap() < MAX_BITS,
ts.bools.is_empty() || *ts.bools.iter().max().unwrap() < MAX_BITS,
"can't double all boolean types"
);
}
@@ -203,7 +203,7 @@ impl TypeVar {
}
}
return TypeVar {
TypeVar {
content: Rc::new(RefCell::new(TypeVarContent {
name: format!("{}({})", derived_func.name(), self.name),
doc: "".into(),
@@ -213,29 +213,29 @@ impl TypeVar {
derived_func,
}),
})),
};
}
}
pub fn lane_of(&self) -> TypeVar {
return self.derived(DerivedFunc::LaneOf);
self.derived(DerivedFunc::LaneOf)
}
pub fn as_bool(&self) -> TypeVar {
return self.derived(DerivedFunc::AsBool);
self.derived(DerivedFunc::AsBool)
}
pub fn half_width(&self) -> TypeVar {
return self.derived(DerivedFunc::HalfWidth);
self.derived(DerivedFunc::HalfWidth)
}
pub fn double_width(&self) -> TypeVar {
return self.derived(DerivedFunc::DoubleWidth);
self.derived(DerivedFunc::DoubleWidth)
}
pub fn half_vector(&self) -> TypeVar {
return self.derived(DerivedFunc::HalfVector);
self.derived(DerivedFunc::HalfVector)
}
pub fn double_vector(&self) -> TypeVar {
return self.derived(DerivedFunc::DoubleVector);
self.derived(DerivedFunc::DoubleVector)
}
pub fn to_bitvec(&self) -> TypeVar {
return self.derived(DerivedFunc::ToBitVec);
self.derived(DerivedFunc::ToBitVec)
}
/// Constrain the range of types this variable can assume to a subset of those in the typeset
@@ -347,7 +347,7 @@ pub enum DerivedFunc {
}
impl DerivedFunc {
pub fn name(&self) -> &'static str {
pub fn name(self) -> &'static str {
match self {
DerivedFunc::LaneOf => "lane_of",
DerivedFunc::AsBool => "as_bool",
@@ -360,7 +360,7 @@ impl DerivedFunc {
}
/// Returns the inverse function of this one, if it is a bijection.
pub fn inverse(&self) -> Option<DerivedFunc> {
pub fn inverse(self) -> Option<DerivedFunc> {
match self {
DerivedFunc::HalfWidth => Some(DerivedFunc::DoubleWidth),
DerivedFunc::DoubleWidth => Some(DerivedFunc::HalfWidth),
@@ -476,7 +476,7 @@ impl TypeSet {
copy.floats = NumSet::new();
copy.refs = NumSet::new();
copy.bitvecs = NumSet::new();
if (&self.lanes - &num_set![1]).len() > 0 {
if !(&self.lanes - &num_set![1]).is_empty() {
copy.bools = &self.ints | &self.floats;
copy.bools = &copy.bools | &self.bools;
}
@@ -512,7 +512,7 @@ impl TypeSet {
.iter()
.filter(|&&x| x < MAX_BITS)
.map(|&x| x * 2)
.filter(legal_bool),
.filter(|x| legal_bool(*x)),
);
copy.bitvecs = NumSet::from_iter(
self.bitvecs
@@ -600,7 +600,7 @@ impl TypeSet {
fn get_singleton(&self) -> ValueType {
let mut types = self.concrete_types();
assert_eq!(types.len(), 1);
return types.remove(0);
types.remove(0)
}
/// Return the inverse image of self across the derived function func.
@@ -615,7 +615,7 @@ impl TypeSet {
let mut copy = self.clone();
copy.bitvecs = NumSet::new();
copy.lanes =
NumSet::from_iter((0..MAX_LANES.trailing_zeros() + 1).map(|i| u16::pow(2, i)));
NumSet::from_iter((0..=MAX_LANES.trailing_zeros()).map(|i| u16::pow(2, i)));
copy
}
DerivedFunc::AsBool => {
@@ -724,11 +724,11 @@ impl TypeSet {
}
fn set_wider_or_equal(s1: &NumSet, s2: &NumSet) -> bool {
s1.len() > 0 && s2.len() > 0 && s1.iter().min() >= s2.iter().max()
!s1.is_empty() && !s2.is_empty() && s1.iter().min() >= s2.iter().max()
}
fn set_narrower(s1: &NumSet, s2: &NumSet) -> bool {
s1.len() > 0 && s2.len() > 0 && s1.iter().min() < s2.iter().max()
!s1.is_empty() && !s2.is_empty() && s1.iter().min() < s2.iter().max()
}
impl fmt::Debug for TypeSet {
@@ -854,7 +854,7 @@ impl TypeSetBuilder {
let bools = range_to_set(self.bools.to_range(1..MAX_BITS, None))
.into_iter()
.filter(legal_bool)
.filter(|x| legal_bool(*x))
.collect();
TypeSet::new(
@@ -921,9 +921,9 @@ impl Into<Interval> for Range {
}
}
fn legal_bool(bits: &RangeBound) -> bool {
fn legal_bool(bits: RangeBound) -> bool {
// Only allow legal bit widths for bool types.
*bits == 1 || (*bits >= 8 && *bits <= MAX_BITS && bits.is_power_of_two())
bits == 1 || (bits >= 8 && bits <= MAX_BITS && bits.is_power_of_two())
}
/// Generates a set with all the powers of two included in the range.
@@ -939,7 +939,7 @@ fn range_to_set(range: Option<Range>) -> NumSet {
assert!(high.is_power_of_two());
assert!(low <= high);
for i in low.trailing_zeros()..high.trailing_zeros() + 1 {
for i in low.trailing_zeros()..=high.trailing_zeros() {
assert!(1 << i <= RangeBound::max_value());
set.insert(1 << i);
}

View File

@@ -255,6 +255,7 @@ fn rewrite_expr(
Apply::new(apply_target, args)
}
#[allow(clippy::too_many_arguments)]
fn rewrite_def_list(
position: PatternPosition,
dummy_defs: Vec<DummyDef>,