Skip to content

Commit de97c5c

Browse files
committed
stronger clippy
1 parent a476dda commit de97c5c

File tree

14 files changed

+146
-193
lines changed

14 files changed

+146
-193
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ rustdoc.all = "warn"
3333
[workspace.lints.clippy]
3434
all = { level = "warn", priority = -1 }
3535
nursery = { level = "warn", priority = -1 }
36+
pedantic = { level = "warn", priority = -1 }
3637
doc_markdown = "allow"
3738
cast_possible_truncation = "allow"
3839
cast_precision_loss = "allow"
@@ -45,11 +46,9 @@ suboptimal_flops = "allow"
4546
cast_sign_loss = "allow"
4647
redundant_pub_crate = "allow"
4748
too_many_lines = "allow"
48-
to_string_trait_impl = "allow"
4949
transmute_ptr_to_ptr = "allow"
5050
missing_transmute_annotations = "allow"
5151
type_complexity = "allow"
52-
needless_range_loop = "allow"
5352
too_many_arguments = "allow"
5453
result_large_err = "allow"
5554
format_push_string = "allow"

crates/air/src/prove.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ pub fn prove_many_air_3<
7070
n_tables,
7171
witnesses_1.len() + witnesses_2.len() + witnesses_3.len()
7272
);
73-
for i in 0..tables_1.len() {
73+
for witness in witnesses_1.iter().take(tables_1.len()) {
7474
assert!(
75-
univariate_skips < witnesses_1[i].log_n_rows(),
75+
univariate_skips < witness.log_n_rows(),
7676
"TODO handle the case UNIVARIATE_SKIPS >= log_length"
7777
);
7878
}
@@ -471,7 +471,7 @@ fn open_structured_columns<EF: ExtensionField<PF<EF>> + ExtensionField<IF>, IF:
471471
);
472472

473473
let mut evaluations_remaining_to_prove = vec![];
474-
for i in 0..n_groups {
474+
for (i, inner_eval) in all_inner_evals.iter().enumerate().take(n_groups) {
475475
let group = &witness.column_groups[i];
476476
let point = MultilinearPoint(
477477
[
@@ -480,7 +480,7 @@ fn open_structured_columns<EF: ExtensionField<PF<EF>> + ExtensionField<IF>, IF:
480480
]
481481
.concat(),
482482
);
483-
let value = all_inner_evals[i][1];
483+
let value = inner_eval[1];
484484
evaluations_remaining_to_prove.push(Evaluation { point, value });
485485
}
486486
evaluations_remaining_to_prove

crates/compiler/src/a_simplify_lang.rs

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::collections::{BTreeMap, BTreeSet};
1+
use std::{
2+
collections::{BTreeMap, BTreeSet},
3+
fmt::{self, Display, Formatter},
4+
};
25

36
use utils::ToUsize;
47

@@ -62,7 +65,7 @@ impl TryInto<VarOrConstMallocAccess> for SimpleExpr {
6265
malloc_label,
6366
offset,
6467
}),
65-
_ => Err(()),
68+
Self::Constant(_) => Err(()),
6669
}
6770
}
6871
}
@@ -1242,6 +1245,11 @@ fn replace_vars_by_const_in_lines(lines: &mut [Line], map: &BTreeMap<Var, F>) {
12421245
}
12431246
Line::FunctionCall {
12441247
args, return_data, ..
1248+
}
1249+
| Line::Precompile {
1250+
precompile: _,
1251+
args,
1252+
res: return_data,
12451253
} => {
12461254
for arg in args {
12471255
replace_vars_by_const_in_expr(arg, map);
@@ -1285,18 +1293,6 @@ fn replace_vars_by_const_in_lines(lines: &mut [Line], map: &BTreeMap<Var, F>) {
12851293
replace_vars_by_const_in_expr(ret, map);
12861294
}
12871295
}
1288-
Line::Precompile {
1289-
precompile: _,
1290-
args,
1291-
res: return_data,
1292-
} => {
1293-
for arg in args {
1294-
replace_vars_by_const_in_expr(arg, map);
1295-
}
1296-
for r in return_data {
1297-
assert!(!map.contains_key(r), "Return variable {r} is a constant");
1298-
}
1299-
}
13001296
Line::Print { content, .. } => {
13011297
for var in content {
13021298
replace_vars_by_const_in_expr(var, map);
@@ -1315,21 +1311,21 @@ fn replace_vars_by_const_in_lines(lines: &mut [Line], map: &BTreeMap<Var, F>) {
13151311
}
13161312
}
13171313

1318-
impl ToString for SimpleLine {
1319-
fn to_string(&self) -> String {
1320-
self.to_string_with_indent(0)
1314+
impl Display for SimpleLine {
1315+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1316+
f.write_str(&self.to_string_with_indent(0))
13211317
}
13221318
}
13231319

1324-
impl ToString for VarOrConstMallocAccess {
1325-
fn to_string(&self) -> String {
1320+
impl Display for VarOrConstMallocAccess {
1321+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
13261322
match self {
1327-
Self::Var(var) => var.to_string(),
1323+
Self::Var(var) => f.write_str(var),
13281324
Self::ConstMallocAccess {
13291325
malloc_label,
13301326
offset,
13311327
} => {
1332-
format!("ConstMallocAccess({malloc_label}, {offset})")
1328+
write!(f, "ConstMallocAccess({malloc_label}, {offset})")
13331329
}
13341330
}
13351331
}
@@ -1345,7 +1341,7 @@ impl SimpleLine {
13451341
arg0,
13461342
arg1,
13471343
} => {
1348-
format!("{} = {} {} {}", var.to_string(), arg0, operation, arg1)
1344+
format!("{var} = {arg0} {operation} {arg1}")
13491345
}
13501346
Self::DecomposeBits {
13511347
var: result,
@@ -1463,45 +1459,46 @@ impl SimpleLine {
14631459
}
14641460
}
14651461

1466-
impl ToString for SimpleFunction {
1467-
fn to_string(&self) -> String {
1462+
impl Display for SimpleFunction {
1463+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
14681464
let args_str = self
14691465
.arguments
14701466
.iter()
14711467
.map(std::string::ToString::to_string)
14721468
.collect::<Vec<_>>()
14731469
.join(", ");
14741470

1475-
let instructions_str = self
1476-
.instructions
1477-
.iter()
1478-
.map(|line| line.to_string_with_indent(1))
1479-
.collect::<Vec<_>>()
1480-
.join("\n");
1481-
14821471
if self.instructions.is_empty() {
1483-
format!(
1472+
write!(
1473+
f,
14841474
"fn {}({}) -> {} {{}}",
14851475
self.name, args_str, self.n_returned_vars
14861476
)
14871477
} else {
1488-
format!(
1489-
"fn {}({}) -> {} {{\n{}\n}}",
1490-
self.name, args_str, self.n_returned_vars, instructions_str
1491-
)
1478+
writeln!(
1479+
f,
1480+
"fn {}({}) -> {} {{",
1481+
self.name, args_str, self.n_returned_vars
1482+
)?;
1483+
for (i, line) in self.instructions.iter().enumerate() {
1484+
if i > 0 {
1485+
writeln!(f)?;
1486+
}
1487+
write!(f, "{}", line.to_string_with_indent(1))?;
1488+
}
1489+
write!(f, "\n}}")
14921490
}
14931491
}
14941492
}
14951493

1496-
impl ToString for SimpleProgram {
1497-
fn to_string(&self) -> String {
1498-
let mut result = String::new();
1494+
impl Display for SimpleProgram {
1495+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
14991496
for (i, function) in self.functions.values().enumerate() {
15001497
if i > 0 {
1501-
result.push('\n');
1498+
writeln!(f)?;
15021499
}
1503-
result.push_str(&function.to_string());
1500+
write!(f, "{function}")?;
15041501
}
1505-
result
1502+
Ok(())
15061503
}
15071504
}

crates/compiler/src/b_compile_intermediate.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ fn compile_lines(
397397
args,
398398
res,
399399
} => {
400-
compile_poseidon(&mut instructions, args, res, compiler, declared_vars, true)?;
400+
compile_poseidon(&mut instructions, args, res, compiler, declared_vars, true);
401401
}
402402

403403
SimpleLine::Precompile {
@@ -409,7 +409,7 @@ fn compile_lines(
409409
args,
410410
res,
411411
} => {
412-
compile_poseidon(&mut instructions, args, res, compiler, declared_vars, false)?;
412+
compile_poseidon(&mut instructions, args, res, compiler, declared_vars, false);
413413
}
414414
SimpleLine::Precompile {
415415
precompile:
@@ -616,7 +616,7 @@ fn compile_poseidon(
616616
compiler: &Compiler,
617617
declared_vars: &mut BTreeSet<Var>,
618618
over_16: bool, // otherwise over_24
619-
) -> Result<(), String> {
619+
) {
620620
assert_eq!(args.len(), 2);
621621
assert_eq!(res.len(), 1);
622622
let res_var = &res[0];
@@ -647,8 +647,6 @@ fn compile_poseidon(
647647
res: low_level_res,
648648
});
649649
}
650-
651-
Ok(())
652650
}
653651

654652
fn compile_function_ret(

crates/compiler/src/c_compile_final.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl IntermediateValue {
306306
offset: eval_const_expression_usize(offset, compiler),
307307
}),
308308
Self::Fp => Ok(MemOrFp::Fp),
309-
_ => Err(format!("Cannot convert {self:?} to MemOrFp")),
309+
Self::Constant(_) => Err(format!("Cannot convert {self:?} to MemOrFp")),
310310
}
311311
}
312312

crates/compiler/src/intermediate_bytecode/operation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub enum HighLevelOperation {
1414

1515
impl HighLevelOperation {
1616
#[must_use]
17-
pub fn eval(&self, a: F, b: F) -> F {
17+
pub fn eval(self, a: F, b: F) -> F {
1818
match self {
1919
Self::Add => a + b,
2020
Self::Mul => a * b,

crates/compiler/src/lang/line.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ impl Line {
133133
} => {
134134
let args_str = args
135135
.iter()
136-
.map(|arg| arg.to_string())
136+
.map(ToString::to_string)
137137
.collect::<Vec<_>>()
138138
.join(", ");
139139
let return_data_str = return_data
140140
.iter()
141-
.map(|var| var.to_string())
141+
.map(ToString::to_string)
142142
.collect::<Vec<_>>()
143143
.join(", ");
144144

@@ -151,7 +151,7 @@ impl Line {
151151
Self::FunctionRet { return_data } => {
152152
let return_data_str = return_data
153153
.iter()
154-
.map(|arg| arg.to_string())
154+
.map(ToString::to_string)
155155
.collect::<Vec<_>>()
156156
.join(", ");
157157
format!("return {return_data_str}")
@@ -165,12 +165,12 @@ impl Line {
165165
"{} = {}({})",
166166
return_data
167167
.iter()
168-
.map(|var| var.to_string())
168+
.map(ToString::to_string)
169169
.collect::<Vec<_>>()
170170
.join(", "),
171171
precompile.name,
172172
args.iter()
173-
.map(|arg| arg.to_string())
173+
.map(ToString::to_string)
174174
.collect::<Vec<_>>()
175175
.join(", ")
176176
)
@@ -181,7 +181,7 @@ impl Line {
181181
} => {
182182
let content_str = content
183183
.iter()
184-
.map(|c| c.to_string())
184+
.map(ToString::to_string)
185185
.collect::<Vec<_>>()
186186
.join(", ");
187187
format!("print({content_str})")

crates/pcs/src/ring_switch.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -222,29 +222,29 @@ impl<F: Field, const D: usize> TensorAlgebra<F, D> {
222222
let p1_split = p1.as_basis_coefficients_slice();
223223
let mut data = [[F::ZERO; D]; D];
224224
for i in 0..D {
225-
for j in 0..D {
226-
data[i][j] = p0_split[i] * p1_split[j];
225+
for (j, &p1) in p1_split.iter().enumerate().take(D) {
226+
data[i][j] = p0_split[i] * p1;
227227
}
228228
}
229229
Self(data)
230230
}
231231

232232
fn rows<EF: ExtensionField<F>>(&self) -> [EF; D] {
233233
let mut result = [EF::ZERO; D];
234-
for i in 0..D {
235-
result[i] = EF::from_basis_coefficients_slice(&self.0[i]).unwrap();
234+
for (i, r) in result.iter_mut().enumerate().take(D) {
235+
*r = EF::from_basis_coefficients_slice(&self.0[i]).unwrap();
236236
}
237237
result
238238
}
239239

240240
fn columns<EF: ExtensionField<F>>(&self) -> [EF; D] {
241241
let mut result = [EF::ZERO; D];
242-
for j in 0..D {
242+
for (j, res) in result.iter_mut().enumerate().take(D) {
243243
let mut col = [F::ZERO; D];
244-
for i in 0..D {
245-
col[i] = self.0[i][j];
244+
for (i, c) in col.iter_mut().enumerate().take(D) {
245+
*c = self.0[i][j];
246246
}
247-
result[j] = EF::from_basis_coefficients_slice(&col).unwrap();
247+
*res = EF::from_basis_coefficients_slice(&col).unwrap();
248248
}
249249
result
250250
}
@@ -259,8 +259,8 @@ impl<F: Field, const D: usize> TensorAlgebra<F, D> {
259259

260260
fn from_columns<EF: ExtensionField<F>>(columns: &[EF; D]) -> Self {
261261
let mut data = [[F::ZERO; D]; D];
262-
for j in 0..D {
263-
let col = columns[j].as_basis_coefficients_slice();
262+
for (j, column) in columns.iter().enumerate().take(D) {
263+
let col = column.as_basis_coefficients_slice();
264264
for i in 0..D {
265265
data[i][j] = col[i];
266266
}
@@ -309,9 +309,9 @@ impl<F: Field, const D: usize> Add for TensorAlgebra<F, D> {
309309

310310
fn add(self, other: Self) -> Self::Output {
311311
let mut result = [[F::ZERO; D]; D];
312-
for i in 0..D {
313-
for j in 0..D {
314-
result[i][j] = self.0[i][j] + other.0[i][j];
312+
for (i, res) in result.iter_mut().enumerate().take(D) {
313+
for (j, r) in res.iter_mut().enumerate().take(D) {
314+
*r = self.0[i][j] + other.0[i][j];
315315
}
316316
}
317317
Self(result)

crates/vm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ pcs.workspace = true
3030
p3-poseidon2-air.workspace = true
3131
lookup.workspace = true
3232
sumcheck.workspace = true
33+
thiserror.workspace = true

0 commit comments

Comments
 (0)