Skip to content

Commit 85ca280

Browse files
authored
Apply clippy suggestions to switch to let chains (#6126)
1 parent 48d8031 commit 85ca280

Some content is hidden

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

53 files changed

+644
-673
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ members = [
138138
version = "0.4.0"
139139
authors = ["RustPython Team"]
140140
edition = "2024"
141-
rust-version = "1.87.0"
141+
rust-version = "1.89.0"
142142
repository = "https://github.com/RustPython/RustPython"
143143
license = "MIT"
144144

common/src/fileutils.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,13 @@ pub mod windows {
8181
.next_back()
8282
.and_then(|s| String::from_utf16(s).ok());
8383

84-
if let Some(file_extension) = file_extension {
85-
if file_extension.eq_ignore_ascii_case("exe")
84+
if let Some(file_extension) = file_extension
85+
&& (file_extension.eq_ignore_ascii_case("exe")
8686
|| file_extension.eq_ignore_ascii_case("bat")
8787
|| file_extension.eq_ignore_ascii_case("cmd")
88-
|| file_extension.eq_ignore_ascii_case("com")
89-
{
90-
self.st_mode |= 0o111;
91-
}
88+
|| file_extension.eq_ignore_ascii_case("com"))
89+
{
90+
self.st_mode |= 0o111;
9291
}
9392
}
9493
}

compiler/codegen/src/compile.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -942,12 +942,11 @@ impl Compiler {
942942
if stack_size > self.symbol_table_stack.len() {
943943
// We might be in a situation where symbol table isn't pushed yet
944944
// In this case, check the parent symbol table
945-
if let Some(parent_table) = self.symbol_table_stack.last() {
946-
if let Some(symbol) = parent_table.lookup(&current_obj_name) {
947-
if symbol.scope == SymbolScope::GlobalExplicit {
948-
force_global = true;
949-
}
950-
}
945+
if let Some(parent_table) = self.symbol_table_stack.last()
946+
&& let Some(symbol) = parent_table.lookup(&current_obj_name)
947+
&& symbol.scope == SymbolScope::GlobalExplicit
948+
{
949+
force_global = true;
951950
}
952951
} else if let Some(_current_table) = self.symbol_table_stack.last() {
953952
// Mangle the name if necessary (for private names in classes)
@@ -956,10 +955,10 @@ impl Compiler {
956955
// Look up in parent symbol table to check scope
957956
if self.symbol_table_stack.len() >= 2 {
958957
let parent_table = &self.symbol_table_stack[self.symbol_table_stack.len() - 2];
959-
if let Some(symbol) = parent_table.lookup(&mangled_name) {
960-
if symbol.scope == SymbolScope::GlobalExplicit {
961-
force_global = true;
962-
}
958+
if let Some(symbol) = parent_table.lookup(&mangled_name)
959+
&& symbol.scope == SymbolScope::GlobalExplicit
960+
{
961+
force_global = true;
963962
}
964963
}
965964
}
@@ -3528,10 +3527,10 @@ impl Compiler {
35283527
}
35293528

35303529
// Validate rest pattern: '_' cannot be used as a rest target
3531-
if let Some(rest) = star_target {
3532-
if rest.as_str() == "_" {
3533-
return Err(self.error(CodegenErrorType::SyntaxError("invalid syntax".to_string())));
3534-
}
3530+
if let Some(rest) = star_target
3531+
&& rest.as_str() == "_"
3532+
{
3533+
return Err(self.error(CodegenErrorType::SyntaxError("invalid syntax".to_string())));
35353534
}
35363535

35373536
// Step 1: Check if subject is a mapping
@@ -5377,11 +5376,11 @@ impl Compiler {
53775376
}
53785377

53795378
fn emit_return_value(&mut self) {
5380-
if let Some(inst) = self.current_block().instructions.last_mut() {
5381-
if let Instruction::LoadConst { idx } = inst.instr {
5382-
inst.instr = Instruction::ReturnConst { idx };
5383-
return;
5384-
}
5379+
if let Some(inst) = self.current_block().instructions.last_mut()
5380+
&& let Instruction::LoadConst { idx } = inst.instr
5381+
{
5382+
inst.instr = Instruction::ReturnConst { idx };
5383+
return;
53855384
}
53865385
emit!(self, Instruction::ReturnValue)
53875386
}

compiler/codegen/src/symboltable.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -240,21 +240,21 @@ fn analyze_symbol_table(symbol_table: &mut SymbolTable) -> SymbolTableResult {
240240
*/
241241
fn drop_class_free(symbol_table: &mut SymbolTable) {
242242
// Check if __class__ is used as a free variable
243-
if let Some(class_symbol) = symbol_table.symbols.get("__class__") {
244-
if class_symbol.scope == SymbolScope::Free {
245-
symbol_table.needs_class_closure = true;
246-
// Note: In CPython, the symbol is removed from the free set,
247-
// but in RustPython we handle this differently during code generation
248-
}
243+
if let Some(class_symbol) = symbol_table.symbols.get("__class__")
244+
&& class_symbol.scope == SymbolScope::Free
245+
{
246+
symbol_table.needs_class_closure = true;
247+
// Note: In CPython, the symbol is removed from the free set,
248+
// but in RustPython we handle this differently during code generation
249249
}
250250

251251
// Check if __classdict__ is used as a free variable
252-
if let Some(classdict_symbol) = symbol_table.symbols.get("__classdict__") {
253-
if classdict_symbol.scope == SymbolScope::Free {
254-
symbol_table.needs_classdict = true;
255-
// Note: In CPython, the symbol is removed from the free set,
256-
// but in RustPython we handle this differently during code generation
257-
}
252+
if let Some(classdict_symbol) = symbol_table.symbols.get("__classdict__")
253+
&& classdict_symbol.scope == SymbolScope::Free
254+
{
255+
symbol_table.needs_classdict = true;
256+
// Note: In CPython, the symbol is removed from the free set,
257+
// but in RustPython we handle this differently during code generation
258258
}
259259
}
260260

@@ -733,12 +733,12 @@ impl SymbolTableBuilder {
733733

734734
fn scan_statement(&mut self, statement: &Stmt) -> SymbolTableResult {
735735
use ruff_python_ast::*;
736-
if let Stmt::ImportFrom(StmtImportFrom { module, names, .. }) = &statement {
737-
if module.as_ref().map(|id| id.as_str()) == Some("__future__") {
738-
for feature in names {
739-
if &feature.name == "annotations" {
740-
self.future_annotations = true;
741-
}
736+
if let Stmt::ImportFrom(StmtImportFrom { module, names, .. }) = &statement
737+
&& module.as_ref().map(|id| id.as_str()) == Some("__future__")
738+
{
739+
for feature in names {
740+
if &feature.name == "annotations" {
741+
self.future_annotations = true;
742742
}
743743
}
744744
}
@@ -1032,26 +1032,23 @@ impl SymbolTableBuilder {
10321032
use ruff_python_ast::*;
10331033

10341034
// Check for expressions not allowed in type parameters scope
1035-
if let Some(table) = self.tables.last() {
1036-
if table.typ == CompilerScope::TypeParams {
1037-
if let Some(keyword) = match expression {
1038-
Expr::Yield(_) | Expr::YieldFrom(_) => Some("yield"),
1039-
Expr::Await(_) => Some("await"),
1040-
Expr::Named(_) => Some("named"),
1041-
_ => None,
1042-
} {
1043-
return Err(SymbolTableError {
1044-
error: format!(
1045-
"{keyword} expression cannot be used within a type parameter"
1046-
),
1047-
location: Some(
1048-
self.source_file
1049-
.to_source_code()
1050-
.source_location(expression.range().start()),
1051-
),
1052-
});
1053-
}
1035+
if let Some(table) = self.tables.last()
1036+
&& table.typ == CompilerScope::TypeParams
1037+
&& let Some(keyword) = match expression {
1038+
Expr::Yield(_) | Expr::YieldFrom(_) => Some("yield"),
1039+
Expr::Await(_) => Some("await"),
1040+
Expr::Named(_) => Some("named"),
1041+
_ => None,
10541042
}
1043+
{
1044+
return Err(SymbolTableError {
1045+
error: format!("{keyword} expression cannot be used within a type parameter"),
1046+
location: Some(
1047+
self.source_file
1048+
.to_source_code()
1049+
.source_location(expression.range().start()),
1050+
),
1051+
});
10551052
}
10561053

10571054
match expression {

derive-impl/src/compile_bytecode.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ impl CompilationSource {
140140
let mut code_map = HashMap::new();
141141
let paths = fs::read_dir(path)
142142
.or_else(|e| {
143-
if cfg!(windows) {
144-
if let Ok(real_path) = fs::read_to_string(path.canonicalize().unwrap()) {
145-
return fs::read_dir(real_path.trim());
146-
}
143+
if cfg!(windows)
144+
&& let Ok(real_path) = fs::read_to_string(path.canonicalize().unwrap())
145+
{
146+
return fs::read_dir(real_path.trim());
147147
}
148148
Err(e)
149149
})
@@ -195,14 +195,14 @@ impl CompilationSource {
195195
})
196196
};
197197
let code = compile_path(&path).or_else(|e| {
198-
if cfg!(windows) {
199-
if let Ok(real_path) = fs::read_to_string(path.canonicalize().unwrap()) {
200-
let joined = path.parent().unwrap().join(real_path.trim());
201-
if joined.exists() {
202-
return compile_path(&joined);
203-
} else {
204-
return Err(e);
205-
}
198+
if cfg!(windows)
199+
&& let Ok(real_path) = fs::read_to_string(path.canonicalize().unwrap())
200+
{
201+
let joined = path.parent().unwrap().join(real_path.trim());
202+
if joined.exists() {
203+
return compile_path(&joined);
204+
} else {
205+
return Err(e);
206206
}
207207
}
208208
Err(e)

jit/src/instructions.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,11 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
175175
let target_block = self.get_or_create_block(label);
176176

177177
// If the current block isn't terminated, jump:
178-
if let Some(cur) = self.builder.current_block() {
179-
if cur != target_block && self.builder.func.layout.last_inst(cur).is_none() {
180-
self.builder.ins().jump(target_block, &[]);
181-
}
178+
if let Some(cur) = self.builder.current_block()
179+
&& cur != target_block
180+
&& self.builder.func.layout.last_inst(cur).is_none()
181+
{
182+
self.builder.ins().jump(target_block, &[]);
182183
}
183184
// Switch to the target block
184185
if self.builder.current_block() != Some(target_block) {
@@ -207,10 +208,10 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
207208
}
208209

209210
// After processing, if the current block is unterminated, insert a trap or fallthrough
210-
if let Some(cur) = self.builder.current_block() {
211-
if self.builder.func.layout.last_inst(cur).is_none() {
212-
self.builder.ins().trap(TrapCode::user(0).unwrap());
213-
}
211+
if let Some(cur) = self.builder.current_block()
212+
&& self.builder.func.layout.last_inst(cur).is_none()
213+
{
214+
self.builder.ins().trap(TrapCode::user(0).unwrap());
214215
}
215216
Ok(())
216217
}

pylib/build.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ fn main() {
99
process_python_libs("./Lib/**/*");
1010
}
1111

12-
if cfg!(windows) {
13-
if let Ok(real_path) = std::fs::read_to_string("Lib") {
14-
let canonicalized_path = std::fs::canonicalize(real_path)
15-
.expect("failed to resolve RUSTPYTHONPATH during build time");
16-
println!(
17-
"cargo:rustc-env=win_lib_path={}",
18-
canonicalized_path.to_str().unwrap()
19-
);
20-
}
12+
if cfg!(windows)
13+
&& let Ok(real_path) = std::fs::read_to_string("Lib")
14+
{
15+
let canonicalized_path = std::fs::canonicalize(real_path)
16+
.expect("failed to resolve RUSTPYTHONPATH during build time");
17+
println!(
18+
"cargo:rustc-env=win_lib_path={}",
19+
canonicalized_path.to_str().unwrap()
20+
);
2121
}
2222
}
2323

src/shell.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ fn shell_exec(
6767
{
6868
let loc = raw_location.start().to_usize();
6969
let mut iter = source.chars();
70-
if let Some(quote) = iter.nth(loc) {
71-
if iter.next() == Some(quote) && iter.next() == Some(quote) {
72-
return ShellExecResult::ContinueLine;
73-
}
70+
if let Some(quote) = iter.nth(loc)
71+
&& iter.next() == Some(quote)
72+
&& iter.next() == Some(quote)
73+
{
74+
return ShellExecResult::ContinueLine;
7475
}
7576
};
7677

stdlib/src/contextvars.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,11 @@ mod _contextvars {
378378
let ctx = ctxs.last()?;
379379
let cached_ptr = zelf.cached.as_ptr();
380380
debug_assert!(!cached_ptr.is_null());
381-
if let Some(cached) = unsafe { &*cached_ptr } {
382-
if zelf.cached_id.load(Ordering::SeqCst) == ctx.get_id()
383-
&& cached.idx + 1 == ctxs.len()
384-
{
385-
return Some(cached.object.clone());
386-
}
381+
if let Some(cached) = unsafe { &*cached_ptr }
382+
&& zelf.cached_id.load(Ordering::SeqCst) == ctx.get_id()
383+
&& cached.idx + 1 == ctxs.len()
384+
{
385+
return Some(cached.object.clone());
387386
}
388387
let vars = ctx.borrow_vars();
389388
let obj = vars.get(zelf)?;

stdlib/src/math.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -518,23 +518,23 @@ mod math {
518518
#[pyfunction]
519519
fn ceil(x: PyObjectRef, vm: &VirtualMachine) -> PyResult {
520520
let result_or_err = try_magic_method(identifier!(vm, __ceil__), vm, &x);
521-
if result_or_err.is_err() {
522-
if let Some(v) = x.try_float_opt(vm) {
523-
let v = try_f64_to_bigint(v?.to_f64().ceil(), vm)?;
524-
return Ok(vm.ctx.new_int(v).into());
525-
}
521+
if result_or_err.is_err()
522+
&& let Some(v) = x.try_float_opt(vm)
523+
{
524+
let v = try_f64_to_bigint(v?.to_f64().ceil(), vm)?;
525+
return Ok(vm.ctx.new_int(v).into());
526526
}
527527
result_or_err
528528
}
529529

530530
#[pyfunction]
531531
fn floor(x: PyObjectRef, vm: &VirtualMachine) -> PyResult {
532532
let result_or_err = try_magic_method(identifier!(vm, __floor__), vm, &x);
533-
if result_or_err.is_err() {
534-
if let Some(v) = x.try_float_opt(vm) {
535-
let v = try_f64_to_bigint(v?.to_f64().floor(), vm)?;
536-
return Ok(vm.ctx.new_int(v).into());
537-
}
533+
if result_or_err.is_err()
534+
&& let Some(v) = x.try_float_opt(vm)
535+
{
536+
let v = try_f64_to_bigint(v?.to_f64().floor(), vm)?;
537+
return Ok(vm.ctx.new_int(v).into());
538538
}
539539
result_or_err
540540
}

0 commit comments

Comments
 (0)