Skip to content

Commit 23e076a

Browse files
authored
Clippy ref_option (#8122)
1 parent 3e047bd commit 23e076a

9 files changed

Lines changed: 45 additions & 44 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,15 @@ iter_filter_is_some = "warn"
403403
manual_is_variant_and = "warn"
404404
map_unwrap_or = "warn"
405405
match_bool = "warn"
406+
mismatching_type_param_order = "warn"
406407
must_use_candidate = "warn"
407408
mut_mut = "warn"
408409
needless_bitwise_bool = "warn"
409410
needless_for_each = "warn"
410411
option_as_ref_cloned = "warn"
411412
ptr_offset_by_literal = "warn"
412413
redundant_else = "warn"
414+
ref_option = "warn"
413415
return_self_not_must_use = "warn"
414416
uninlined_format_args = "warn"
415417
unnecessary_wraps = "warn"

crates/derive-impl/src/compile_bytecode.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl PyCompileArgs {
227227
let mut source: Option<CompilationSource> = None;
228228
let mut crate_name = None;
229229

230-
fn assert_source_empty(source: &Option<CompilationSource>) -> Result<(), syn::Error> {
230+
fn assert_source_empty(source: Option<&CompilationSource>) -> Result<(), syn::Error> {
231231
if let Some(source) = source {
232232
Err(syn::Error::new(
233233
source.span.0,
@@ -263,14 +263,14 @@ impl PyCompileArgs {
263263
} else if ident == "module_name" {
264264
module_name = Some(check_str()?.value())
265265
} else if ident == "source" {
266-
assert_source_empty(&source)?;
266+
assert_source_empty(source.as_ref())?;
267267
let code = check_str()?.value();
268268
source = Some(CompilationSource {
269269
kind: CompilationSourceKind::SourceCode(code),
270270
span: (ident.span(), meta.input.cursor().span()),
271271
});
272272
} else if ident == "file" {
273-
assert_source_empty(&source)?;
273+
assert_source_empty(source.as_ref())?;
274274
let (base, rel_path) = str_path()?;
275275
source = Some(CompilationSource {
276276
kind: CompilationSourceKind::File { base, rel_path },
@@ -281,7 +281,7 @@ impl PyCompileArgs {
281281
bail_span!(ident, "py_compile doesn't accept dir")
282282
}
283283

284-
assert_source_empty(&source)?;
284+
assert_source_empty(source.as_ref())?;
285285
let (base, rel_path) = str_path()?;
286286
source = Some(CompilationSource {
287287
kind: CompilationSourceKind::Dir { base, rel_path },

crates/stdlib/src/socket.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ mod _socket {
10851085
loop {
10861086
if deadline.is_some() || matches!(wait_kind, SockWaitKind::Connect) {
10871087
let sock = self.sock()?;
1088-
sock_wait_deadline(&sock, wait_kind, &deadline, vm)?;
1088+
sock_wait_deadline(&sock, wait_kind, deadline.as_ref(), vm)?;
10891089
}
10901090

10911091
let err = loop {
@@ -2452,7 +2452,7 @@ mod _socket {
24522452
timeout: Option<Duration>,
24532453
vm: &VirtualMachine,
24542454
) -> PyResult<bool> {
2455-
match sock_wait_deadline(sock, wait_kind, &timeout.map(Deadline::new), vm) {
2455+
match sock_wait_deadline(sock, wait_kind, timeout.map(Deadline::new).as_ref(), vm) {
24562456
Ok(()) => Ok(false),
24572457
Err(IoOrPyException::Timeout) => Ok(true),
24582458
Err(e) => Err(e.into_pyexception(vm)),
@@ -2463,7 +2463,7 @@ mod _socket {
24632463
fn sock_wait_deadline(
24642464
sock: &Socket,
24652465
wait_kind: SockWaitKind,
2466-
deadline: &Option<Deadline>,
2466+
deadline: Option<&Deadline>,
24672467
vm: &VirtualMachine,
24682468
) -> Result<(), IoOrPyException> {
24692469
#[cfg(unix)]

crates/vm/src/builtins/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl Py<PyModule> {
180180
.flatten()
181181
.filter(|s| !vm.is_none(s));
182182

183-
let origin = get_spec_file_origin(&spec, vm);
183+
let origin = get_spec_file_origin(spec.as_ref(), vm);
184184

185185
let is_possibly_shadowing = origin
186186
.as_ref()

crates/vm/src/builtins/type.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,8 @@ impl PyType {
806806
// Note: inherit_slots is called in PyClassImpl::init_class after
807807
// slots are fully initialized by make_slots()
808808

809-
Self::set_new(&new_type.slots, &new_type.base);
810-
Self::set_alloc(&new_type.slots, &new_type.base);
809+
Self::set_new(&new_type.slots, new_type.base.as_ref());
810+
Self::set_alloc(&new_type.slots, new_type.base.as_ref());
811811

812812
let weakref_type = super::PyWeak::static_type();
813813
for base in new_type.bases.read().iter() {
@@ -853,25 +853,23 @@ impl PyType {
853853
self.update_slot::<true>(attr_name, ctx);
854854
}
855855

856-
Self::set_new(&self.slots, &self.base);
857-
Self::set_alloc(&self.slots, &self.base);
856+
Self::set_new(&self.slots, self.base.as_ref());
857+
Self::set_alloc(&self.slots, self.base.as_ref());
858858
}
859859

860-
fn set_new(slots: &PyTypeSlots, base: &Option<PyTypeRef>) {
860+
fn set_new(slots: &PyTypeSlots, base: Option<&PyTypeRef>) {
861861
if slots.flags.contains(PyTypeFlags::DISALLOW_INSTANTIATION) {
862862
slots.new.store(None)
863863
} else if slots.new.load().is_none() {
864-
slots
865-
.new
866-
.store(base.as_ref().and_then(|base| base.slots.new.load()))
864+
slots.new.store(base.and_then(|base| base.slots.new.load()))
867865
}
868866
}
869867

870-
fn set_alloc(slots: &PyTypeSlots, base: &Option<PyTypeRef>) {
868+
fn set_alloc(slots: &PyTypeSlots, base: Option<&PyTypeRef>) {
871869
if slots.alloc.load().is_none() {
872870
slots
873871
.alloc
874-
.store(base.as_ref().and_then(|base| base.slots.alloc.load()));
872+
.store(base.and_then(|base| base.slots.alloc.load()));
875873
}
876874
}
877875

crates/vm/src/frame.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,11 @@ impl LocalsPlus {
340340

341341
/// Get a reference to a stack slot by index from the bottom.
342342
#[inline(always)]
343-
fn stack_index(&self, idx: usize) -> &Option<PyStackRef> {
343+
fn stack_index(&self, idx: usize) -> Option<&PyStackRef> {
344344
debug_assert!(idx < self.stack_top as usize);
345345
let data = self.data_as_slice();
346346
let raw_idx = self.nlocalsplus as usize + idx;
347-
unsafe { &*(data.as_ptr().add(raw_idx) as *const Option<PyStackRef>) }
347+
unsafe { (*(data.as_ptr().add(raw_idx) as *const Option<PyStackRef>)).as_ref() }
348348
}
349349

350350
/// Get a mutable reference to a stack slot by index from the bottom.
@@ -358,7 +358,7 @@ impl LocalsPlus {
358358

359359
/// Get the last stack element (top of stack).
360360
#[inline(always)]
361-
fn stack_last(&self) -> Option<&Option<PyStackRef>> {
361+
fn stack_last(&self) -> Option<Option<&PyStackRef>> {
362362
if self.stack_top == 0 {
363363
None
364364
} else {
@@ -2343,8 +2343,8 @@ impl ExecutingFrame<'_> {
23432343
let idx = index.get(arg) as usize;
23442344
let stack_len = self.localsplus.stack_len();
23452345
debug_assert!(stack_len >= idx, "CopyItem: stack underflow");
2346-
let value = self.localsplus.stack_index(stack_len - idx).clone();
2347-
self.push_stackref_opt(value);
2346+
let value = self.localsplus.stack_index(stack_len - idx);
2347+
self.push_stackref_opt(value.cloned());
23482348
Ok(None)
23492349
}
23502350
Instruction::CopyFreeVars { n } => {
@@ -3567,10 +3567,10 @@ impl ExecutingFrame<'_> {
35673567

35683568
let stack_len = self.localsplus.stack_len();
35693569
let exit_func = expect_unchecked(
3570-
self.localsplus.stack_index(stack_len - 5).clone(),
3570+
self.localsplus.stack_index(stack_len - 5),
35713571
"WithExceptStart: exit_func is NULL",
35723572
);
3573-
let self_or_null = self.localsplus.stack_index(stack_len - 4).clone();
3573+
let self_or_null = self.localsplus.stack_index(stack_len - 4);
35743574

35753575
let (tp, val, tb) = if let Some(ref exc) = exc {
35763576
vm.split_exception(exc.clone())
@@ -3579,7 +3579,7 @@ impl ExecutingFrame<'_> {
35793579
};
35803580

35813581
let exit_res = if let Some(self_exit) = self_or_null {
3582-
exit_func.call((self_exit.to_pyobj(), tp, val, tb), vm)?
3582+
exit_func.call((self_exit.clone().to_pyobj(), tp, val, tb), vm)?
35833583
} else {
35843584
exit_func.call((tp, val, tb), vm)?
35853585
};
@@ -6233,7 +6233,7 @@ impl ExecutingFrame<'_> {
62336233
.ok()
62346234
.filter(|s| !vm.is_none(s));
62356235

6236-
let origin = get_spec_file_origin(&spec, vm);
6236+
let origin = get_spec_file_origin(spec.as_ref(), vm);
62376237

62386238
let is_possibly_shadowing = origin
62396239
.as_ref()

crates/vm/src/getpath.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ pub fn init_path_config(settings: &Settings) -> Paths {
132132
};
133133

134134
// Step 2: Check for venv (pyvenv.cfg) and get 'home'
135-
let (venv_prefix, home_dir) = detect_venv(&exe_dir);
135+
let (venv_prefix, home_dir) = detect_venv(exe_dir.as_ref());
136136
let search_dir = home_dir.clone().or(exe_dir);
137137

138138
// Step 3: Check for build directory
139-
let build_prefix = detect_build_directory(&search_dir);
139+
let build_prefix = detect_build_directory(search_dir.as_ref());
140140

141141
// Step 4: Calculate prefix via landmark search
142142
// When in venv, search_dir is home_dir, so this gives us the base Python's prefix
143-
let calculated_prefix = calculate_prefix(&search_dir, &build_prefix);
143+
let calculated_prefix = calculate_prefix(search_dir.as_ref(), build_prefix.as_ref());
144144

145145
// Step 5: Set prefix and base_prefix
146146
if venv_prefix.is_some() {
@@ -161,13 +161,13 @@ pub fn init_path_config(settings: &Settings) -> Paths {
161161
// In venv: exec_prefix = prefix (venv directory)
162162
paths.prefix.clone()
163163
} else {
164-
calculate_exec_prefix(&search_dir, &paths.prefix)
164+
calculate_exec_prefix(search_dir.as_ref(), paths.prefix.as_ref())
165165
};
166166
paths.base_exec_prefix = paths.base_prefix.clone();
167167

168168
// Step 7: Calculate base_executable (if not already set by __PYVENV_LAUNCHER__)
169169
if paths.base_executable.is_empty() {
170-
paths.base_executable = calculate_base_executable(executable.as_ref(), &home_dir);
170+
paths.base_executable = calculate_base_executable(executable.as_ref(), home_dir.as_ref());
171171
}
172172

173173
// Step 8: Build module_search_paths
@@ -195,7 +195,7 @@ fn default_prefix() -> String {
195195

196196
/// Detect virtual environment by looking for pyvenv.cfg
197197
/// Returns (venv_prefix, home_dir from pyvenv.cfg)
198-
fn detect_venv(exe_dir: &Option<PathBuf>) -> (Option<PathBuf>, Option<PathBuf>) {
198+
fn detect_venv(exe_dir: Option<&PathBuf>) -> (Option<PathBuf>, Option<PathBuf>) {
199199
// Try exe_dir/../pyvenv.cfg first (standard venv layout: venv/bin/python)
200200
if let Some(dir) = exe_dir
201201
&& let Some(venv_dir) = dir.parent()
@@ -222,8 +222,8 @@ fn detect_venv(exe_dir: &Option<PathBuf>) -> (Option<PathBuf>, Option<PathBuf>)
222222
}
223223

224224
/// Detect if running from a build directory
225-
fn detect_build_directory(exe_dir: &Option<PathBuf>) -> Option<PathBuf> {
226-
let dir = exe_dir.as_ref()?;
225+
fn detect_build_directory(exe_dir: Option<&PathBuf>) -> Option<PathBuf> {
226+
let dir = exe_dir?;
227227

228228
// Check for pybuilddir.txt (indicates build directory)
229229
if dir.join(platform::BUILDDIR_TXT).exists() {
@@ -240,7 +240,7 @@ fn detect_build_directory(exe_dir: &Option<PathBuf>) -> Option<PathBuf> {
240240
}
241241

242242
/// Calculate prefix by searching for landmarks
243-
fn calculate_prefix(exe_dir: &Option<PathBuf>, build_prefix: &Option<PathBuf>) -> String {
243+
fn calculate_prefix(exe_dir: Option<&PathBuf>, build_prefix: Option<&PathBuf>) -> String {
244244
// 1. If build directory detected, use it
245245
if let Some(bp) = build_prefix {
246246
return bp.to_string_lossy().into_owned();
@@ -266,7 +266,7 @@ fn calculate_prefix(exe_dir: &Option<PathBuf>, build_prefix: &Option<PathBuf>) -
266266
}
267267

268268
/// Calculate exec_prefix
269-
fn calculate_exec_prefix(exe_dir: &Option<PathBuf>, prefix: &str) -> String {
269+
fn calculate_exec_prefix(exe_dir: Option<&PathBuf>, prefix: &str) -> String {
270270
#[cfg(windows)]
271271
{
272272
// Windows: exec_prefix == prefix
@@ -289,7 +289,7 @@ fn calculate_exec_prefix(exe_dir: &Option<PathBuf>, prefix: &str) -> String {
289289
}
290290

291291
/// Calculate base_executable
292-
fn calculate_base_executable(executable: Option<&PathBuf>, home_dir: &Option<PathBuf>) -> String {
292+
fn calculate_base_executable(executable: Option<&PathBuf>, home_dir: Option<&PathBuf>) -> String {
293293
// If in venv and we have home, construct base_executable from home
294294
if let (Some(exe), Some(home)) = (executable, home_dir)
295295
&& let Some(exe_name) = exe.file_name()

crates/vm/src/import.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,11 @@ pub fn remove_importlib_frames(vm: &VirtualMachine, exc: &Py<PyBaseException>) {
267267

268268
/// Get origin path from a module spec, checking has_location first.
269269
pub(crate) fn get_spec_file_origin(
270-
spec: &Option<PyObjectRef>,
270+
spec: Option<&PyObjectRef>,
271271
vm: &VirtualMachine,
272272
) -> Option<String> {
273-
let spec = spec.as_ref()?;
273+
let spec = spec?;
274+
274275
let has_location = spec
275276
.get_attr("has_location", vm)
276277
.ok()

crates/vm/src/stdlib/_ast/validate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ fn validate_typeparam(vm: &VirtualMachine, tp: &ast::TypeParam) -> PyResult<()>
279279

280280
fn validate_type_params(
281281
vm: &VirtualMachine,
282-
type_params: &Option<Box<ast::TypeParams>>,
282+
type_params: Option<&ast::TypeParams>,
283283
) -> PyResult<()> {
284284
if let Some(type_params) = type_params {
285285
for tp in &type_params.type_params {
@@ -478,7 +478,7 @@ fn validate_stmt(vm: &VirtualMachine, stmt: &ast::Stmt) -> PyResult<()> {
478478
"FunctionDef"
479479
};
480480
validate_body(vm, &func.body, owner)?;
481-
validate_type_params(vm, &func.type_params)?;
481+
validate_type_params(vm, func.type_params.as_deref())?;
482482
validate_parameters(vm, &func.parameters)?;
483483
validate_decorators(vm, &func.decorator_list)?;
484484
if let Some(returns) = &func.returns {
@@ -488,7 +488,7 @@ fn validate_stmt(vm: &VirtualMachine, stmt: &ast::Stmt) -> PyResult<()> {
488488
}
489489
ast::Stmt::ClassDef(class_def) => {
490490
validate_body(vm, &class_def.body, "ClassDef")?;
491-
validate_type_params(vm, &class_def.type_params)?;
491+
validate_type_params(vm, class_def.type_params.as_deref())?;
492492
if let Some(arguments) = &class_def.arguments {
493493
validate_exprs(vm, &arguments.args, ast::ExprContext::Load, false)?;
494494
validate_keywords(vm, &arguments.keywords)?;
@@ -525,7 +525,7 @@ fn validate_stmt(vm: &VirtualMachine, stmt: &ast::Stmt) -> PyResult<()> {
525525
return Err(vm.new_type_error("TypeAlias with non-Name name"));
526526
}
527527
validate_expr(vm, &alias.name, ast::ExprContext::Store)?;
528-
validate_type_params(vm, &alias.type_params)?;
528+
validate_type_params(vm, alias.type_params.as_deref())?;
529529
validate_expr(vm, &alias.value, ast::ExprContext::Load)
530530
}
531531
ast::Stmt::For(for_stmt) => {

0 commit comments

Comments
 (0)