Skip to content

Commit 5d37c52

Browse files
a4lgAmanieu
authored andcommitted
stdarch-gen-arm: Modernization of the coding style
It modernizes the coding style of the crate stdarch-gen-arm by fixing Clippy warnings (except clippy::{collapsible_if,obfuscated_if_else} that might make the program look worse as a result of "fixing" warnings). Clippy: rust version 1.89.0-nightly (6f6971078 2025-05-28) Number of Fixed Warnings: 84/84 Note: Rust Analyzer double counts one of the Clippy warnings so it reduces 85 warnings (as reported by the Rust Analyzer). This commit also applies similar technique used to resolve Clippy warnings but also simplifies identifier name formatting and makes reading easier. Confirmed that the exact same code will be generated.
1 parent e2f8dca commit 5d37c52

File tree

9 files changed

+76
-170
lines changed

9 files changed

+76
-170
lines changed

crates/stdarch-gen-arm/src/big_endian.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn create_array(lanes: u32) -> Option<String> {
3838
4 => Some("[3, 2, 1, 0]".to_string()),
3939
8 => Some("[7, 6, 5, 4, 3, 2, 1, 0]".to_string()),
4040
16 => Some("[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]".to_string()),
41-
_ => panic!("Incorrect vector number of vector lanes: {}", lanes),
41+
_ => panic!("Incorrect vector number of vector lanes: {lanes}"),
4242
}
4343
}
4444

@@ -78,12 +78,7 @@ pub fn type_has_tuple(type_kind: &TypeKind) -> bool {
7878
}
7979

8080
pub fn make_variable_mutable(variable_name: &str, type_kind: &TypeKind) -> Expression {
81-
let mut_variable = format!(
82-
"let mut {}: {} = {}",
83-
variable_name,
84-
type_kind.to_string(),
85-
variable_name
86-
);
81+
let mut_variable = format!("let mut {variable_name}: {type_kind} = {variable_name}");
8782
let identifier_name = create_single_wild_string(&mut_variable);
8883
Expression::Identifier(identifier_name, IdentifierType::Symbol)
8984
}
@@ -114,9 +109,7 @@ fn create_shuffle_internal(
114109
};
115110

116111
let lane_count = vector_type.lanes();
117-
let Some(array_lanes) = create_array(lane_count) else {
118-
return None;
119-
};
112+
let array_lanes = create_array(lane_count)?;
120113

121114
let tuple_count = vector_type.tuple_size().map_or_else(|| 0, |t| t.to_int());
122115

@@ -144,10 +137,7 @@ fn create_assigned_tuple_shuffle_call_fmt(
144137
array_lanes: &String,
145138
) -> String {
146139
format!(
147-
"{variable_name}.{idx} = unsafe {{ simd_shuffle!({variable_name}.{idx}, {variable_name}.{idx}, {array_lanes}) }};\n",
148-
variable_name = variable_name,
149-
idx = idx,
150-
array_lanes = array_lanes
140+
"{variable_name}.{idx} = unsafe {{ simd_shuffle!({variable_name}.{idx}, {variable_name}.{idx}, {array_lanes}) }};\n"
151141
)
152142
}
153143

@@ -157,10 +147,7 @@ fn create_assigned_shuffle_call_fmt(
157147
array_lanes: &String,
158148
) -> String {
159149
format!(
160-
"let {variable_name}: {type_kind} = unsafe {{ simd_shuffle!({variable_name}, {variable_name}, {array_lanes}) }}",
161-
type_kind = type_kind.to_string(),
162-
variable_name = variable_name,
163-
array_lanes = array_lanes
150+
"let {variable_name}: {type_kind} = unsafe {{ simd_shuffle!({variable_name}, {variable_name}, {array_lanes}) }}"
164151
)
165152
}
166153

@@ -169,11 +156,7 @@ fn create_shuffle_call_fmt(
169156
_type_kind: &TypeKind,
170157
array_lanes: &String,
171158
) -> String {
172-
format!(
173-
"simd_shuffle!({variable_name}, {variable_name}, {array_lanes})",
174-
variable_name = variable_name,
175-
array_lanes = array_lanes
176-
)
159+
format!("simd_shuffle!({variable_name}, {variable_name}, {array_lanes})")
177160
}
178161

179162
/// Create a `simd_shuffle!(<...>, [...])` call, where the output is stored

crates/stdarch-gen-arm/src/context.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use itertools::Itertools;
22
use serde::{Deserialize, Serialize};
3-
use std::{collections::HashMap, usize};
3+
use std::collections::HashMap;
44

55
use crate::{
66
expression::Expression,
@@ -165,11 +165,12 @@ impl LocalContext {
165165
.map_or_else(err, |ty| Ok((ty.size().parse::<i32>().unwrap()-1).to_string())),
166166
Wildcard::SizeInBytesLog2(idx) => self.input.typekind(*idx)
167167
.map_or_else(err, |ty| Ok(ty.size_in_bytes_log2())),
168-
Wildcard::NVariant if self.substitutions.get(wildcard).is_none() => Ok(String::new()),
168+
Wildcard::NVariant if !self.substitutions.contains_key(wildcard) => Ok(String::new()),
169169
Wildcard::TypeKind(idx, opts) => {
170170
self.input.typekind(*idx)
171171
.map_or_else(err, |ty| {
172172
let literal = if let Some(opts) = opts {
173+
#[allow(clippy::obfuscated_if_else)]
173174
opts.contains(ty.base_type().map(|bt| *bt.kind()).ok_or_else(|| {
174175
format!("cannot retrieve a type literal out of {ty}")
175176
})?)

crates/stdarch-gen-arm/src/expression.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl FnCall {
5656
FnCall(Box::new(fn_ptr), arguments, Vec::new(), true).into()
5757
}
5858

59-
pub fn is_llvm_link_call(&self, llvm_link_name: &String) -> bool {
59+
pub fn is_llvm_link_call(&self, llvm_link_name: &str) -> bool {
6060
self.is_expected_call(llvm_link_name)
6161
}
6262

@@ -66,7 +66,7 @@ impl FnCall {
6666

6767
pub fn is_expected_call(&self, fn_call_name: &str) -> bool {
6868
if let Expression::Identifier(fn_name, IdentifierType::Symbol) = self.0.as_ref() {
69-
&fn_name.to_string() == fn_call_name
69+
fn_name.to_string() == fn_call_name
7070
} else {
7171
false
7272
}
@@ -205,6 +205,7 @@ impl Expression {
205205
Self::FnCall(fn_call) => {
206206
fn_call.build(intrinsic, ctx)?;
207207

208+
#[allow(clippy::collapsible_if)]
208209
if let Some(llvm_link_name) = ctx.local.substitutions.get(&Wildcard::LLVMLink) {
209210
if fn_call.is_llvm_link_call(llvm_link_name) {
210211
*self = intrinsic
@@ -357,19 +358,15 @@ impl Expression {
357358
false
358359
}
359360
}
360-
_ => panic!("Badly defined function call: {:?}", fn_call),
361+
_ => panic!("Badly defined function call: {fn_call:?}"),
361362
},
362363
_ => false,
363364
}
364365
}
365366

366367
/// Determine if an espression is a LLVM binding
367368
pub fn is_llvm_link(&self) -> bool {
368-
if let Expression::LLVMLink(_) = self {
369-
true
370-
} else {
371-
false
372-
}
369+
matches!(self, Expression::LLVMLink(_))
373370
}
374371
}
375372

@@ -508,7 +505,7 @@ impl ToTokens for Expression {
508505
identifier
509506
.to_string()
510507
.parse::<TokenStream>()
511-
.expect(format!("invalid syntax: {:?}", self).as_str())
508+
.unwrap_or_else(|_| panic!("invalid syntax: {self:?}"))
512509
.to_tokens(tokens);
513510
}
514511
Self::IntConstant(n) => tokens.append(Literal::i32_unsuffixed(*n)),

crates/stdarch-gen-arm/src/fn_suffix.rs

Lines changed: 28 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn neon_get_base_and_char(ty: &VectorType) -> (u32, char, bool) {
6161
BaseType::Sized(BaseTypeKind::Int, size) => (*size, 's', *size * lanes == 128),
6262
BaseType::Sized(BaseTypeKind::UInt, size) => (*size, 'u', *size * lanes == 128),
6363
BaseType::Sized(BaseTypeKind::Poly, size) => (*size, 'p', *size * lanes == 128),
64-
_ => panic!("Unhandled {:?}", ty),
64+
_ => panic!("Unhandled {ty:?}"),
6565
}
6666
}
6767

@@ -73,169 +73,92 @@ pub fn make_neon_suffix(type_kind: TypeKind, suffix_kind: SuffixKind) -> String
7373
TypeKind::Vector(ty) => {
7474
let tuple_size = ty.tuple_size().map_or(0, |t| t.to_int());
7575
let (base_size, prefix_char, requires_q) = neon_get_base_and_char(&ty);
76+
let prefix_q = if requires_q { "q" } else { "" };
7677
let lanes = ty.lanes();
7778
match suffix_kind {
7879
SuffixKind::Normal => {
79-
let mut str_suffix: String = String::new();
80-
if requires_q {
81-
str_suffix.push('q');
82-
}
83-
str_suffix.push('_');
84-
str_suffix.push(prefix_char);
85-
str_suffix.push_str(base_size.to_string().as_str());
80+
let mut str_suffix: String = format!("{prefix_q}_{prefix_char}{base_size}");
8681
if tuple_size > 0 {
8782
str_suffix.push_str("_x");
8883
str_suffix.push_str(tuple_size.to_string().as_str());
8984
}
90-
return str_suffix;
85+
str_suffix
9186
}
9287
SuffixKind::NSuffix => {
93-
let mut str_suffix: String = String::new();
94-
if requires_q {
95-
str_suffix.push('q');
96-
}
97-
str_suffix.push_str("_n_");
98-
str_suffix.push(prefix_char);
99-
str_suffix.push_str(base_size.to_string().as_str());
100-
return str_suffix;
88+
format!("{prefix_q}_n_{prefix_char}{base_size}")
10189
}
10290

103-
SuffixKind::NoQ => format!("_{}{}", prefix_char, base_size),
104-
SuffixKind::NoQNSuffix => format!("_n{}{}", prefix_char, base_size),
91+
SuffixKind::NoQ => format!("_{prefix_char}{base_size}"),
92+
SuffixKind::NoQNSuffix => format!("_n{prefix_char}{base_size}"),
10593

10694
SuffixKind::Unsigned => {
10795
let t = type_kind.to_string();
10896
if t.starts_with("u") {
10997
return t;
11098
}
111-
return format!("u{}", t);
99+
format!("u{t}")
112100
}
113101
SuffixKind::Lane => {
114102
if lanes == 0 {
115-
panic!("type {} has no lanes!", type_kind.to_string())
103+
panic!("type {type_kind} has no lanes!")
116104
} else {
117-
format!("{}", lanes)
105+
format!("{lanes}")
118106
}
119107
}
120108
SuffixKind::Tuple => {
121109
if tuple_size == 0 {
122-
panic!("type {} has no lanes!", type_kind.to_string())
110+
panic!("type {type_kind} has no lanes!")
123111
} else {
124-
format!("{}", tuple_size)
112+
format!("{tuple_size}")
125113
}
126114
}
127115
SuffixKind::Base => base_size.to_string(),
128116
SuffixKind::NoX => {
129-
let mut str_suffix: String = String::new();
130-
if requires_q {
131-
str_suffix.push('q');
132-
}
133-
str_suffix.push('_');
134-
str_suffix.push(prefix_char);
135-
str_suffix.push_str(base_size.to_string().as_str());
136-
return str_suffix;
117+
format!("{prefix_q}_{prefix_char}{base_size}")
137118
}
138119
SuffixKind::Dup => {
139-
let mut str_suffix: String = String::new();
140-
if requires_q {
141-
str_suffix.push('q');
142-
}
143-
str_suffix.push('_');
144-
str_suffix.push_str("dup_");
145-
str_suffix.push(prefix_char);
146-
str_suffix.push_str(base_size.to_string().as_str());
120+
let mut str_suffix: String = format!("{prefix_q}_dup_{prefix_char}{base_size}");
147121
if tuple_size > 0 {
148122
str_suffix.push_str("_x");
149123
str_suffix.push_str(tuple_size.to_string().as_str());
150124
}
151-
return str_suffix;
125+
str_suffix
152126
}
153127
SuffixKind::DupNox => {
154-
let mut str_suffix: String = String::new();
155-
if requires_q {
156-
str_suffix.push('q');
157-
}
158-
str_suffix.push('_');
159-
str_suffix.push_str("dup_");
160-
str_suffix.push(prefix_char);
161-
str_suffix.push_str(base_size.to_string().as_str());
162-
return str_suffix;
128+
format!("{prefix_q}_dup_{prefix_char}{base_size}")
163129
}
164130
SuffixKind::LaneNoX => {
165-
let mut str_suffix: String = String::new();
166-
if requires_q {
167-
str_suffix.push('q');
168-
}
169-
str_suffix.push('_');
170-
str_suffix.push_str("lane_");
171-
str_suffix.push(prefix_char);
172-
str_suffix.push_str(base_size.to_string().as_str());
173-
return str_suffix;
131+
format!("{prefix_q}_lane_{prefix_char}{base_size}")
174132
}
175133
SuffixKind::LaneQNoX => {
176-
let mut str_suffix: String = String::new();
177-
if requires_q {
178-
str_suffix.push('q');
179-
}
180-
str_suffix.push('_');
181-
str_suffix.push_str("laneq_");
182-
str_suffix.push(prefix_char);
183-
str_suffix.push_str(base_size.to_string().as_str());
184-
return str_suffix;
134+
format!("{prefix_q}_laneq_{prefix_char}{base_size}")
185135
}
186136
SuffixKind::Rot270 => {
187-
if requires_q {
188-
return format!("q_rot270_{}{}", prefix_char, base_size.to_string());
189-
}
190-
return format!("_rot270_{}{}", prefix_char, base_size.to_string());
137+
format!("{prefix_q}_rot270_{prefix_char}{base_size}")
191138
}
192139
SuffixKind::Rot270Lane => {
193-
if requires_q {
194-
return format!("q_rot270_lane_{}{}", prefix_char, base_size.to_string());
195-
}
196-
return format!("_rot270_lane_{}{}", prefix_char, base_size.to_string());
140+
format!("{prefix_q}_rot270_lane_{prefix_char}{base_size}")
197141
}
198142
SuffixKind::Rot270LaneQ => {
199-
if requires_q {
200-
return format!("q_rot270_laneq_{}{}", prefix_char, base_size.to_string());
201-
}
202-
return format!("_rot270_laneq_{}{}", prefix_char, base_size.to_string());
143+
format!("{prefix_q}_rot270_laneq_{prefix_char}{base_size}")
203144
}
204145
SuffixKind::Rot180 => {
205-
if requires_q {
206-
return format!("q_rot180_{}{}", prefix_char, base_size.to_string());
207-
}
208-
return format!("_rot180_{}{}", prefix_char, base_size.to_string());
146+
format!("{prefix_q}_rot180_{prefix_char}{base_size}")
209147
}
210148
SuffixKind::Rot180Lane => {
211-
if requires_q {
212-
return format!("q_rot180_lane_{}{}", prefix_char, base_size.to_string());
213-
}
214-
return format!("_rot180_lane_{}{}", prefix_char, base_size.to_string());
149+
format!("{prefix_q}_rot180_lane_{prefix_char}{base_size}")
215150
}
216151
SuffixKind::Rot180LaneQ => {
217-
if requires_q {
218-
return format!("q_rot180_laneq_{}{}", prefix_char, base_size.to_string());
219-
}
220-
return format!("_rot180_laneq_{}{}", prefix_char, base_size.to_string());
152+
format!("{prefix_q}_rot180_laneq_{prefix_char}{base_size}")
221153
}
222154
SuffixKind::Rot90 => {
223-
if requires_q {
224-
return format!("q_rot90_{}{}", prefix_char, base_size.to_string());
225-
}
226-
return format!("_rot90_{}{}", prefix_char, base_size.to_string());
155+
format!("{prefix_q}_rot90_{prefix_char}{base_size}")
227156
}
228157
SuffixKind::Rot90Lane => {
229-
if requires_q {
230-
return format!("q_rot90_lane_{}{}", prefix_char, base_size.to_string());
231-
}
232-
return format!("_rot90_lane_{}{}", prefix_char, base_size.to_string());
158+
format!("{prefix_q}_rot90_lane_{prefix_char}{base_size}")
233159
}
234160
SuffixKind::Rot90LaneQ => {
235-
if requires_q {
236-
return format!("q_rot90_laneq_{}{}", prefix_char, base_size.to_string());
237-
}
238-
return format!("_rot90_laneq_{}{}", prefix_char, base_size.to_string());
161+
format!("{prefix_q}_rot90_laneq_{prefix_char}{base_size}")
239162
}
240163
SuffixKind::BaseByteSize => format!("{}", base_size / 8),
241164
}
@@ -272,7 +195,7 @@ impl FromStr for SuffixKind {
272195
"base_byte_size" => Ok(SuffixKind::BaseByteSize),
273196
"lane_nox" => Ok(SuffixKind::LaneNoX),
274197
"laneq_nox" => Ok(SuffixKind::LaneQNoX),
275-
_ => Err(format!("unknown suffix type: {}", s)),
198+
_ => Err(format!("unknown suffix type: {s}")),
276199
}
277200
}
278201
}

0 commit comments

Comments
 (0)