Skip to content

Commit 460b1d3

Browse files
authored
Clippy warn uninlined_format_args & redundant_else (#7873)
1 parent 11e991f commit 460b1d3

67 files changed

Lines changed: 323 additions & 355 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,6 @@ inconsistent_struct_constructor = "warn"
358358
manual_is_variant_and = "warn"
359359
map_unwrap_or = "warn"
360360
must_use_candidate = "warn"
361+
redundant_else = "warn"
362+
uninlined_format_args = "warn"
361363
unnecessary_wraps = "warn"

crates/codegen/src/compile.rs

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ pub fn compile_program(
289289
) -> CompileResult<CodeObject> {
290290
let symbol_table = SymbolTable::scan_program(ast, source_file.clone())
291291
.map_err(|e| e.into_codegen_error(source_file.name().to_owned()))?;
292-
let mut compiler = Compiler::new(opts, source_file, "<module>".to_owned());
292+
let mut compiler = Compiler::new(opts, source_file, "<module>");
293293
compiler.compile_program(ast, symbol_table)?;
294294
let code = compiler.exit_scope();
295295
trace!("Compilation completed: {code:?}");
@@ -304,7 +304,7 @@ pub fn compile_program_single(
304304
) -> CompileResult<CodeObject> {
305305
let symbol_table = SymbolTable::scan_program(ast, source_file.clone())
306306
.map_err(|e| e.into_codegen_error(source_file.name().to_owned()))?;
307-
let mut compiler = Compiler::new(opts, source_file, "<module>".to_owned());
307+
let mut compiler = Compiler::new(opts, source_file, "<module>");
308308
compiler.compile_program_single(&ast.body, symbol_table)?;
309309
let code = compiler.exit_scope();
310310
trace!("Compilation completed: {code:?}");
@@ -318,7 +318,7 @@ pub fn compile_block_expression(
318318
) -> CompileResult<CodeObject> {
319319
let symbol_table = SymbolTable::scan_program(ast, source_file.clone())
320320
.map_err(|e| e.into_codegen_error(source_file.name().to_owned()))?;
321-
let mut compiler = Compiler::new(opts, source_file, "<module>".to_owned());
321+
let mut compiler = Compiler::new(opts, source_file, "<module>");
322322
compiler.compile_block_expr(&ast.body, symbol_table)?;
323323
let code = compiler.exit_scope();
324324
trace!("Compilation completed: {code:?}");
@@ -332,7 +332,7 @@ pub fn compile_expression(
332332
) -> CompileResult<CodeObject> {
333333
let symbol_table = SymbolTable::scan_expr(ast, source_file.clone())
334334
.map_err(|e| e.into_codegen_error(source_file.name().to_owned()))?;
335-
let mut compiler = Compiler::new(opts, source_file, "<module>".to_owned());
335+
let mut compiler = Compiler::new(opts, source_file, "<module>");
336336
compiler.compile_eval(ast, symbol_table)?;
337337
let code = compiler.exit_scope();
338338
Ok(code)
@@ -447,13 +447,14 @@ impl PatternContext {
447447
}
448448
}
449449

450+
#[derive(Clone, Copy, Eq, PartialEq)]
450451
enum JumpOp {
451452
Jump,
452453
PopJumpIfFalse,
453454
}
454455

455456
/// Type of collection to build in starunpack_helper
456-
#[derive(Debug, Clone, Copy, PartialEq)]
457+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
457458
enum CollectionType {
458459
Tuple,
459460
List,
@@ -541,7 +542,7 @@ impl Compiler {
541542
}
542543
}
543544

544-
fn new(opts: CompileOpts, source_file: SourceFile, code_name: String) -> Self {
545+
fn new(opts: CompileOpts, source_file: SourceFile, code_name: &str) -> Self {
545546
let module_code = ir::CodeInfo {
546547
// CPython convention: top-level module / interactive /
547548
// expression code does not carry CO_NEWLOCALS or CO_OPTIMIZED.
@@ -558,8 +559,8 @@ impl Compiler {
558559
current_block: BlockIdx::new(0),
559560
annotations_blocks: None,
560561
metadata: ir::CodeUnitMetadata {
561-
name: code_name.clone(),
562-
qualname: Some(code_name),
562+
name: code_name.to_string(),
563+
qualname: Some(code_name.to_string()),
563564
consts: IndexSet::default(),
564565
names: IndexSet::default(),
565566
varnames: IndexSet::default(),
@@ -1287,8 +1288,7 @@ impl Compiler {
12871288
let name = current_table.name.clone();
12881289
let typ = current_table.typ;
12891290
return Err(self.error(CodegenErrorType::SyntaxError(format!(
1290-
"no symbol table available in {} (type: {:?})",
1291-
name, typ
1291+
"no symbol table available in {name} (type: {typ:?})"
12921292
))));
12931293
}
12941294

@@ -1829,7 +1829,7 @@ impl Compiler {
18291829
posonlyarg_count: u32,
18301830
arg_count: u32,
18311831
kwonlyarg_count: u32,
1832-
obj_name: String,
1832+
obj_name: &str,
18331833
) -> CompileResult<()> {
18341834
// First push the symbol table
18351835
let table = self.push_symbol_table()?;
@@ -1842,7 +1842,7 @@ impl Compiler {
18421842
let lineno = self.get_source_line_number().get();
18431843

18441844
// Call enter_scope which does most of the work
1845-
self.enter_scope(&obj_name, scope_type, key, lineno.to_u32())?;
1845+
self.enter_scope(obj_name, scope_type, key, lineno.to_u32())?;
18461846

18471847
// Override the values that push_output sets explicitly
18481848
// enter_scope sets default values based on scope_type, but push_output
@@ -3400,7 +3400,7 @@ impl Compiler {
34003400
parameters.posonlyargs.len().to_u32(),
34013401
(parameters.posonlyargs.len() + parameters.args.len()).to_u32(),
34023402
parameters.kwonlyargs.len().to_u32(),
3403-
name.to_owned(),
3403+
name,
34043404
)?;
34053405

34063406
let args_iter = core::iter::empty()
@@ -5258,7 +5258,7 @@ impl Compiler {
52585258
0,
52595259
num_typeparam_args as u32,
52605260
0,
5261-
type_params_name,
5261+
&type_params_name,
52625262
)?;
52635263

52645264
// TypeParams scope is function-like
@@ -5820,7 +5820,7 @@ impl Compiler {
58205820
0,
58215821
0,
58225822
0,
5823-
type_params_name,
5823+
&type_params_name,
58245824
)?;
58255825

58265826
// Set private name for name mangling
@@ -7956,21 +7956,20 @@ impl Compiler {
79567956
if let ast::Expr::Starred(_) = &element {
79577957
if seen_star {
79587958
return Err(self.error(CodegenErrorType::MultipleStarArgs));
7959-
} else {
7960-
seen_star = true;
7961-
let before = i;
7962-
let after = elts.len() - i - 1;
7963-
let (before, after) = (|| Some((before.to_u8()?, after.to_u8()?)))(
7964-
)
7959+
}
7960+
7961+
seen_star = true;
7962+
let before = i;
7963+
let after = elts.len() - i - 1;
7964+
let (before, after) = (|| Some((before.to_u8()?, after.to_u8()?)))()
79657965
.ok_or_else(|| {
79667966
self.error_ranged(
79677967
CodegenErrorType::TooManyStarUnpack,
79687968
target.range(),
79697969
)
79707970
})?;
7971-
let counts = bytecode::UnpackExArgs { before, after };
7972-
emit!(self, Instruction::UnpackEx { counts });
7973-
}
7971+
let counts = bytecode::UnpackExArgs { before, after };
7972+
emit!(self, Instruction::UnpackEx { counts });
79747973
}
79757974
}
79767975

@@ -9696,8 +9695,7 @@ impl Compiler {
96969695
let name = current_table.name.clone();
96979696
let typ = current_table.typ;
96989697
Err(self.error(CodegenErrorType::SyntaxError(format!(
9699-
"no symbol table available in {} (type: {:?})",
9700-
name, typ
9698+
"no symbol table available in {name} (type: {typ:?})"
97019699
))))
97029700
}
97039701
})();
@@ -9715,14 +9713,14 @@ impl Compiler {
97159713
posonlyarg_count: u32,
97169714
arg_count: u32,
97179715
kwonlyarg_count: u32,
9718-
obj_name: String,
9716+
obj_name: &str,
97199717
) -> CompileResult<()> {
97209718
let scope_type = table.typ;
97219719
self.symbol_table_stack.push(table);
97229720

97239721
let key = self.symbol_table_stack.len() - 1;
97249722
let lineno = self.get_source_line_number().get();
9725-
self.enter_scope(&obj_name, scope_type, key, lineno.to_u32())?;
9723+
self.enter_scope(obj_name, scope_type, key, lineno.to_u32())?;
97269724

97279725
if let Some(info) = self.code_stack.last_mut() {
97289726
info.flags = flags | (info.flags & bytecode::CodeFlags::NESTED);
@@ -9786,7 +9784,7 @@ impl Compiler {
97869784
// and relies on the inlined path itself to handle GET_AITER /
97879785
// async-comprehension cleanup.
97889786
return self.compile_inlined_comprehension(
9789-
comp_table,
9787+
&comp_table,
97909788
init_collection,
97919789
generators,
97929790
compile_element,
@@ -9821,7 +9819,7 @@ impl Compiler {
98219819
// scope itself. Peek past those nested scopes so we can enter the
98229820
// correct comprehension table here, then let the real outermost
98239821
// iterator compile consume its nested scopes later in parent scope.
9824-
self.push_output_with_symbol_table(comp_table, flags, 1, 1, 0, name.to_owned())?;
9822+
self.push_output_with_symbol_table(comp_table, flags, 1, 1, 0, name)?;
98259823

98269824
// Set qualname for comprehension
98279825
self.set_qualname();
@@ -10028,7 +10026,7 @@ impl Compiler {
1002810026
/// This generates bytecode inline without creating a new code object
1002910027
fn compile_inlined_comprehension(
1003010028
&mut self,
10031-
comp_table: SymbolTable,
10029+
comp_table: &SymbolTable,
1003210030
init_collection: Option<AnyInstruction>,
1003310031
generators: &[ast::Comprehension],
1003410032
compile_element: &dyn Fn(&mut Self, usize) -> CompileResult<()>,
@@ -12380,7 +12378,7 @@ mod tests {
1238012378
let symbol_table = SymbolTable::scan_program(&ast, source_file.clone())
1238112379
.map_err(|e| e.into_codegen_error(source_file.name().to_owned()))
1238212380
.unwrap();
12383-
let mut compiler = Compiler::new(opts, source_file, "<module>".to_owned());
12381+
let mut compiler = Compiler::new(opts, source_file, "<module>");
1238412382
compiler.compile_program(&ast, symbol_table).unwrap();
1238512383
compiler.exit_scope()
1238612384
}
@@ -12418,7 +12416,7 @@ mod tests {
1241812416
let symbol_table = SymbolTable::scan_program(&ast, source_file.clone())
1241912417
.map_err(|e| e.into_codegen_error(source_file.name().to_owned()))
1242012418
.unwrap();
12421-
let mut compiler = Compiler::new(opts, source_file, "<module>".to_owned());
12419+
let mut compiler = Compiler::new(opts, source_file, "<module>");
1242212420
compiler.compile_program(&ast, symbol_table).unwrap();
1242312421
let _table = compiler.pop_symbol_table();
1242412422
let stack_top = compiler.code_stack.pop().unwrap();
@@ -12459,7 +12457,7 @@ mod tests {
1245912457
let is_async = function.is_async;
1246012458
let range = function.range();
1246112459

12462-
let mut compiler = Compiler::new(opts, source_file, "<module>".to_owned());
12460+
let mut compiler = Compiler::new(opts, source_file, "<module>");
1246312461
compiler.future_annotations = symbol_table.future_annotations;
1246412462
compiler.symbol_table_stack.push(symbol_table);
1246512463
compiler.set_source_range(range);

crates/codegen/src/symboltable.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,7 +2418,7 @@ impl SymbolTableBuilder {
24182418
};
24192419
if !seen_names.insert(name) {
24202420
return Err(SymbolTableError {
2421-
error: format!("duplicate type parameter '{}'", name),
2421+
error: format!("duplicate type parameter '{name}'"),
24222422
location: Some(
24232423
self.source_file
24242424
.to_source_code()
@@ -2431,8 +2431,7 @@ impl SymbolTableBuilder {
24312431
} else if default_seen {
24322432
return Err(SymbolTableError {
24332433
error: format!(
2434-
"non-default type parameter '{}' follows default type parameter",
2435-
name
2434+
"non-default type parameter '{name}' follows default type parameter"
24362435
),
24372436
location: Some(
24382437
self.source_file
@@ -2743,8 +2742,7 @@ impl SymbolTableBuilder {
27432742
{
27442743
return Err(SymbolTableError {
27452744
error: format!(
2746-
"assignment expression cannot rebind comprehension iteration variable '{}'",
2747-
mangled
2745+
"assignment expression cannot rebind comprehension iteration variable '{mangled}'"
27482746
),
27492747
location,
27502748
});
@@ -2868,8 +2866,7 @@ impl SymbolTableBuilder {
28682866
{
28692867
return Err(SymbolTableError {
28702868
error: format!(
2871-
"comprehension inner loop cannot rebind assignment expression target '{}'",
2872-
name
2869+
"comprehension inner loop cannot rebind assignment expression target '{name}'"
28732870
),
28742871
location,
28752872
});

crates/common/src/cformat.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -788,21 +788,23 @@ impl<S> CFormatStrOrBytes<S> {
788788
iter.next().unwrap();
789789
literal.extend([second]);
790790
continue;
791-
} else {
792-
if !literal.is_empty() {
793-
parts.push((
794-
part_index,
795-
CFormatPart::Literal(core::mem::take(&mut literal)),
796-
));
797-
}
798-
let spec = CFormatSpecKeyed::parse(iter).map_err(|err| CFormatError {
799-
typ: err.0,
800-
index: err.1,
801-
})?;
802-
parts.push((index, CFormatPart::Spec(spec)));
803-
if let Some(&(index, _)) = iter.peek() {
804-
part_index = index;
805-
}
791+
}
792+
793+
if !literal.is_empty() {
794+
parts.push((
795+
part_index,
796+
CFormatPart::Literal(core::mem::take(&mut literal)),
797+
));
798+
}
799+
800+
let spec = CFormatSpecKeyed::parse(iter).map_err(|err| CFormatError {
801+
typ: err.0,
802+
index: err.1,
803+
})?;
804+
805+
parts.push((index, CFormatPart::Spec(spec)));
806+
if let Some(&(index, _)) = iter.peek() {
807+
part_index = index;
806808
}
807809
} else {
808810
return Err(CFormatError {

crates/common/src/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ impl FormatSpec {
763763
Ok("inf%".to_owned())
764764
} else {
765765
let capped = float::clamp_fmt_precision(precision);
766-
let mut result = format!("{:.*}", capped, scaled);
766+
let mut result = format!("{scaled:.capped$}");
767767
// Pad with '0's up to the requested precision to match
768768
// CPython byte-identically past the internal cap.
769769
let missing = precision.saturating_sub(capped);

crates/compiler/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl CompileError {
6565
line: loc.line,
6666
character_offset: loc.character_offset.saturating_add(1),
6767
};
68-
let msg = format!("'{}' was never closed", bracket_char);
68+
let msg = format!("'{bracket_char}' was never closed");
6969
is_unclosed_bracket = true;
7070
(parser::ParseErrorType::OtherError(msg), loc, end_loc)
7171
} else {

0 commit comments

Comments
 (0)