Skip to content

Commit

Permalink
Add clippy suggestions (#6203)
Browse files Browse the repository at this point in the history
* add clippy suggestions

* revert &/ref change

* Update cranelift/isle/isle/src/parser.rs

Co-authored-by: Jamey Sharp <[email protected]>

---------

Co-authored-by: Jamey Sharp <[email protected]>
  • Loading branch information
kevaundray and jameysharp authored Apr 17, 2023
1 parent 91de5de commit 85118c8
Show file tree
Hide file tree
Showing 22 changed files with 140 additions and 172 deletions.
8 changes: 4 additions & 4 deletions cranelift/codegen/meta/src/cdsl/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ impl InstructionBuilder {
}

fn build(self) -> Instruction {
let operands_in = self.operands_in.unwrap_or_else(Vec::new);
let operands_out = self.operands_out.unwrap_or_else(Vec::new);
let operands_in = self.operands_in.unwrap_or_default();
let operands_out = self.operands_out.unwrap_or_default();

let mut value_opnums = Vec::new();
let mut imm_opnums = Vec::new();
Expand Down Expand Up @@ -375,7 +375,7 @@ fn verify_polymorphic(
if (free_typevar.is_some() && tv == &free_typevar.unwrap())
|| tv.singleton_type().is_some()
{
match is_ctrl_typevar_candidate(tv, &operands_in, &operands_out) {
match is_ctrl_typevar_candidate(tv, operands_in, operands_out) {
Ok(_other_typevars) => {
return Some(PolymorphicInfo {
use_typevar_operand: true,
Expand Down Expand Up @@ -410,7 +410,7 @@ fn verify_polymorphic(

// At this point, if the next unwrap() fails, it means the output type couldn't be used as a
// controlling type variable either; panicking is the right behavior.
is_ctrl_typevar_candidate(tv, &operands_in, &operands_out).unwrap();
is_ctrl_typevar_candidate(tv, operands_in, operands_out).unwrap();

Some(PolymorphicInfo {
use_typevar_operand: false,
Expand Down
40 changes: 17 additions & 23 deletions cranelift/codegen/meta/src/cdsl/operands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ impl Operand {
}

pub fn is_value(&self) -> bool {
match self.kind.fields {
OperandKindFields::TypeVar(_) => true,
_ => false,
}
matches!(self.kind.fields, OperandKindFields::TypeVar(_))
}

pub fn type_var(&self) -> Option<&TypeVar> {
Expand All @@ -64,28 +61,25 @@ impl Operand {
}

pub fn is_varargs(&self) -> bool {
match self.kind.fields {
OperandKindFields::VariableArgs => true,
_ => false,
}
matches!(self.kind.fields, OperandKindFields::VariableArgs)
}

/// Returns true if the operand has an immediate kind or is an EntityRef.
pub fn is_immediate_or_entityref(&self) -> bool {
match self.kind.fields {
matches!(
self.kind.fields,
OperandKindFields::ImmEnum(_)
| OperandKindFields::ImmValue
| OperandKindFields::EntityRef => true,
_ => false,
}
| OperandKindFields::ImmValue
| OperandKindFields::EntityRef
)
}

/// Returns true if the operand has an immediate kind.
pub fn is_immediate(&self) -> bool {
match self.kind.fields {
OperandKindFields::ImmEnum(_) | OperandKindFields::ImmValue => true,
_ => false,
}
matches!(
self.kind.fields,
OperandKindFields::ImmEnum(_) | OperandKindFields::ImmValue
)
}
}

Expand Down Expand Up @@ -158,18 +152,18 @@ impl OperandKind {
}
}

impl Into<OperandKind> for &TypeVar {
fn into(self) -> OperandKind {
impl From<&TypeVar> for OperandKind {
fn from(type_var: &TypeVar) -> Self {
OperandKind {
rust_field_name: "value",
rust_type: "ir::Value",
fields: OperandKindFields::TypeVar(self.into()),
fields: OperandKindFields::TypeVar(type_var.into()),
doc: None,
}
}
}
impl Into<OperandKind> for &OperandKind {
fn into(self) -> OperandKind {
self.clone()
impl From<&OperandKind> for OperandKind {
fn from(kind: &OperandKind) -> Self {
kind.clone()
}
}
33 changes: 14 additions & 19 deletions cranelift/codegen/meta/src/cdsl/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ pub(crate) enum PresetType {
OtherPreset(PresetIndex),
}

impl Into<PresetType> for BoolSettingIndex {
fn into(self) -> PresetType {
PresetType::BoolSetting(self)
impl From<BoolSettingIndex> for PresetType {
fn from(bool_setting_index: BoolSettingIndex) -> Self {
PresetType::BoolSetting(bool_setting_index)
}
}
impl Into<PresetType> for PresetIndex {
fn into(self) -> PresetType {
PresetType::OtherPreset(self)
impl From<PresetIndex> for PresetType {
fn from(value: PresetIndex) -> Self {
PresetType::OtherPreset(value)
}
}

Expand Down Expand Up @@ -134,13 +134,7 @@ impl SettingGroup {
fn num_bool_settings(&self) -> u8 {
self.settings
.iter()
.filter(|s| {
if let SpecificSetting::Bool(_) = s.specific {
true
} else {
false
}
})
.filter(|s| matches!(s.specific, SpecificSetting::Bool(_)))
.count() as u8
}

Expand Down Expand Up @@ -184,14 +178,15 @@ pub(crate) enum PredicateNode {
And(Box<PredicateNode>, Box<PredicateNode>),
}

impl Into<PredicateNode> for BoolSettingIndex {
fn into(self) -> PredicateNode {
PredicateNode::OwnedBool(self)
impl From<BoolSettingIndex> for PredicateNode {
fn from(bool_setting_index: BoolSettingIndex) -> Self {
PredicateNode::OwnedBool(bool_setting_index)
}
}
impl<'a> Into<PredicateNode> for (BoolSettingIndex, &'a SettingGroup) {
fn into(self) -> PredicateNode {
let (index, group) = (self.0, self.1);

impl<'a> From<(BoolSettingIndex, &'a SettingGroup)> for PredicateNode {
fn from(val: (BoolSettingIndex, &'a SettingGroup)) -> Self {
let (index, group) = (val.0, val.1);
let setting = &group.settings[index.0];
PredicateNode::SharedBool(group.name, setting.name)
}
Expand Down
6 changes: 1 addition & 5 deletions cranelift/codegen/meta/src/cdsl/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,6 @@ impl ReferenceTypeIterator {
impl Iterator for ReferenceTypeIterator {
type Item = ReferenceType;
fn next(&mut self) -> Option<Self::Item> {
if let Some(r) = self.reference_iter.next() {
Some(ReferenceType::from(r))
} else {
None
}
self.reference_iter.next().map(ReferenceType::from)
}
}
28 changes: 11 additions & 17 deletions cranelift/codegen/meta/src/cdsl/typevar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,14 @@ impl TypeVar {
}
}

impl Into<TypeVar> for &TypeVar {
fn into(self) -> TypeVar {
self.clone()
impl From<&TypeVar> for TypeVar {
fn from(type_var: &TypeVar) -> Self {
type_var.clone()
}
}
impl Into<TypeVar> for ValueType {
fn into(self) -> TypeVar {
TypeVar::new_singleton(self)
impl From<ValueType> for TypeVar {
fn from(value_type: ValueType) -> Self {
TypeVar::new_singleton(value_type)
}
}

Expand Down Expand Up @@ -507,7 +507,7 @@ impl TypeSet {
self.dynamic_lanes
.iter()
.filter(|&&x| x < MAX_LANES)
.map(|&x| x),
.copied(),
);
copy.dynamic_lanes = NumSet::new();
copy
Expand Down Expand Up @@ -660,13 +660,7 @@ pub(crate) enum Interval {
impl Interval {
fn to_range(&self, full_range: Range, default: Option<RangeBound>) -> Option<Range> {
match self {
Interval::None => {
if let Some(default_val) = default {
Some(default_val..default_val)
} else {
None
}
}
Interval::None => default.map(|default_val| default_val..default_val),

Interval::All => Some(full_range),

Expand All @@ -683,9 +677,9 @@ impl Interval {
}
}

impl Into<Interval> for Range {
fn into(self) -> Interval {
Interval::Range(self)
impl From<Range> for Interval {
fn from(range: Range) -> Self {
Interval::Range(range)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/meta/src/constant_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn generate_table<'cont, T, I: iter::Iterator<Item = &'cont T>, H: Fn(&T) ->
let mut table = vec![None; size];

for i in items {
let mut h = hash_function(&i) % size;
let mut h = hash_function(i) % size;
let mut s = 0;
while table[h].is_some() {
s += 1;
Expand Down
8 changes: 4 additions & 4 deletions cranelift/codegen/meta/src/gen_inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ fn get_constraint<'entries, 'table>(
if let Some(free_typevar) = type_var.free_typevar() {
if ctrl_typevar.is_some() && free_typevar != *ctrl_typevar.unwrap() {
assert!(type_var.base.is_none());
return format!("Free({})", type_sets.add(&type_var.get_raw_typeset()));
return format!("Free({})", type_sets.add(type_var.get_raw_typeset()));
}
}

Expand Down Expand Up @@ -809,7 +809,7 @@ fn gen_type_constraints(all_inst: &AllInstructions, fmt: &mut Formatter) {
fmt.indent(|fmt| {
for inst in all_inst.iter() {
let (ctrl_typevar, ctrl_typeset) = if let Some(poly) = &inst.polymorphic_info {
let index = type_sets.add(&*poly.ctrl_typevar.get_raw_typeset());
let index = type_sets.add(poly.ctrl_typevar.get_raw_typeset());
(Some(&poly.ctrl_typevar), index)
} else {
(None, TYPESET_LIMIT)
Expand Down Expand Up @@ -857,7 +857,7 @@ fn gen_type_constraints(all_inst: &AllInstructions, fmt: &mut Formatter) {
.collect::<Vec<_>>()
.join(", ")));
if let Some(poly) = &inst.polymorphic_info {
fmt.comment(format!("Polymorphic over {}", typeset_to_string(&poly.ctrl_typevar.get_raw_typeset())));
fmt.comment(format!("Polymorphic over {}", typeset_to_string(poly.ctrl_typevar.get_raw_typeset())));
}

// Compute the bit field encoding, c.f. instructions.rs.
Expand Down Expand Up @@ -1730,7 +1730,7 @@ fn gen_builder(
fmt.line("pub trait InstBuilder<'f>: InstBuilderBase<'f> {");
fmt.indent(|fmt| {
for inst in instructions.iter() {
gen_inst_builder(inst, &*inst.format, fmt);
gen_inst_builder(inst, &inst.format, fmt);
fmt.empty_line();
}
for (i, format) in formats.iter().enumerate() {
Expand Down
14 changes: 7 additions & 7 deletions cranelift/codegen/meta/src/gen_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ fn gen_getters(group: &SettingGroup, fmt: &mut Formatter) {
}

for setting in &group.settings {
gen_getter(&setting, fmt);
gen_getter(setting, fmt);
}
for predicate in &group.predicates {
gen_pred_getter(&predicate, &group, fmt);
gen_pred_getter(predicate, group, fmt);
}
});
fmtln!(fmt, "}");
Expand Down Expand Up @@ -364,8 +364,8 @@ fn gen_descriptors(group: &SettingGroup, fmt: &mut Formatter) {

// Generate hash table.
let mut hash_entries: Vec<SettingOrPreset> = Vec::new();
hash_entries.extend(group.settings.iter().map(|x| SettingOrPreset::Setting(x)));
hash_entries.extend(group.presets.iter().map(|x| SettingOrPreset::Preset(x)));
hash_entries.extend(group.settings.iter().map(SettingOrPreset::Setting));
hash_entries.extend(group.presets.iter().map(SettingOrPreset::Preset));

let hash_table = generate_table(hash_entries.iter(), hash_entries.len(), |entry| {
simple_hash(entry.name())
Expand Down Expand Up @@ -399,9 +399,9 @@ fn gen_descriptors(group: &SettingGroup, fmt: &mut Formatter) {
fmt.comment(format!(
"{}: {}",
preset.name,
preset.setting_names(&group).collect::<Vec<_>>().join(", ")
preset.setting_names(group).collect::<Vec<_>>().join(", ")
));
for (mask, value) in preset.layout(&group) {
for (mask, value) in preset.layout(group) {
fmtln!(fmt, "(0b{:08b}, 0b{:08b}),", mask, value);
}
}
Expand Down Expand Up @@ -502,7 +502,7 @@ pub(crate) fn generate(
out_dir: &str,
) -> Result<(), error::Error> {
let mut fmt = Formatter::new();
gen_group(&settings, parent_group, &mut fmt);
gen_group(settings, parent_group, &mut fmt);
fmt.update_file(filename, out_dir)?;
Ok(())
}
8 changes: 4 additions & 4 deletions cranelift/codegen/meta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ pub fn generate(isas: &[isa::Isa], out_dir: &str, isle_dir: &str) -> Result<(),
&shared_defs.settings,
gen_settings::ParentGroup::None,
"settings.rs",
&out_dir,
out_dir,
)?;
gen_types::generate("types.rs", &out_dir)?;
gen_types::generate("types.rs", out_dir)?;

// - per ISA definitions.
let target_isas = isa::define(isas, &mut shared_defs);
Expand All @@ -49,7 +49,7 @@ pub fn generate(isas: &[isa::Isa], out_dir: &str, isle_dir: &str) -> Result<(),
"inst_builder.rs",
"clif_opt.isle",
"clif_lower.isle",
&out_dir,
out_dir,
isle_dir,
)?;

Expand All @@ -58,7 +58,7 @@ pub fn generate(isas: &[isa::Isa], out_dir: &str, isle_dir: &str) -> Result<(),
&isa.settings,
gen_settings::ParentGroup::Shared,
&format!("settings-{}.rs", isa.name),
&out_dir,
out_dir,
)?;
}

Expand Down
4 changes: 2 additions & 2 deletions cranelift/codegen/meta/src/shared/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Definitions {
// Check name.
if let Some(existing_format) = format_names.get(&inst.format.name) {
assert!(
Rc::ptr_eq(&existing_format, &inst.format),
Rc::ptr_eq(existing_format, &inst.format),
"formats must uniquely named; there's a\
conflict on the name '{}', please make sure it is used only once.",
existing_format.name
Expand All @@ -80,7 +80,7 @@ impl Definitions {
}
}

let mut result = Vec::from_iter(format_structures.into_iter().map(|(_, v)| v));
let mut result = Vec::from_iter(format_structures.into_values());
result.sort_by_key(|format| format.name);
result
}
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/meta/src/srcgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn _indent(s: &str) -> Option<usize> {
fn parse_multiline(s: &str) -> Vec<String> {
// Convert tabs into spaces.
let expanded_tab = format!("{:-1$}", " ", SHIFTWIDTH);
let lines: Vec<String> = s.lines().map(|l| l.replace("\t", &expanded_tab)).collect();
let lines: Vec<String> = s.lines().map(|l| l.replace('\t', &expanded_tab)).collect();

// Determine minimum indentation, ignoring the first line and empty lines.
let indent = lines
Expand Down
2 changes: 1 addition & 1 deletion cranelift/control/src/zero_sized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct ControlPlane {
/// disabled. It doesn't consume any bytes and always returns a default
/// control plane.
impl arbitrary::Arbitrary<'_> for ControlPlane {
fn arbitrary<'a>(_u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
fn arbitrary(_u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
Ok(Self::default())
}
}
Expand Down
Loading

0 comments on commit 85118c8

Please sign in to comment.