Skip to content

Commit 1464d5c

Browse files
authored
Adding + Fixing Clippy rules to better align with #[no_std] (#6570)
* * Added alloc_instead_of_core, std_instead_of_alloc, and std_instead_of_core clippy rules * Manually changed part of the code to use core/alloc * use clippy --fix to fix issues in stdlib * * Used clippy --fix to fix issues in vm * Imported Range in vm/src/anystr.rs * * Used clippy --fix to fix issues in common
1 parent 489289f commit 1464d5c

File tree

185 files changed

+1000
-952
lines changed

Some content is hidden

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

185 files changed

+1000
-952
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ unsafe_op_in_unsafe_fn = "deny"
232232
elided_lifetimes_in_paths = "warn"
233233

234234
[workspace.lints.clippy]
235+
# alloc_instead_of_core = "warn"
236+
# std_instead_of_alloc = "warn"
237+
# std_instead_of_core = "warn"
235238
perf = "warn"
236239
style = "warn"
237240
complexity = "warn"

crates/codegen/src/compile.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::{
1616
symboltable::{self, CompilerScope, SymbolFlags, SymbolScope, SymbolTable},
1717
unparse::UnparseExpr,
1818
};
19+
use alloc::borrow::Cow;
1920
use itertools::Itertools;
2021
use malachite_bigint::BigInt;
2122
use num_complex::Complex;
@@ -42,7 +43,7 @@ use rustpython_compiler_core::{
4243
},
4344
};
4445
use rustpython_wtf8::Wtf8Buf;
45-
use std::{borrow::Cow, collections::HashSet};
46+
use std::collections::HashSet;
4647

4748
const MAXBLOCKS: usize = 20;
4849

@@ -293,7 +294,7 @@ fn compiler_unwrap_option<T>(zelf: &Compiler, o: Option<T>) -> T {
293294
o.unwrap()
294295
}
295296

296-
// fn compiler_result_unwrap<T, E: std::fmt::Debug>(zelf: &Compiler, result: Result<T, E>) -> T {
297+
// fn compiler_result_unwrap<T, E: core::fmt::Debug>(zelf: &Compiler, result: Result<T, E>) -> T {
297298
// if result.is_err() {
298299
// eprintln!("=== CODEGEN PANIC INFO ===");
299300
// eprintln!("This IS an internal error, an result was unwrapped during codegen");
@@ -1831,7 +1832,7 @@ impl Compiler {
18311832
name.to_owned(),
18321833
);
18331834

1834-
let args_iter = std::iter::empty()
1835+
let args_iter = core::iter::empty()
18351836
.chain(&parameters.posonlyargs)
18361837
.chain(&parameters.args)
18371838
.map(|arg| &arg.parameter)
@@ -2438,7 +2439,7 @@ impl Compiler {
24382439
let mut funcflags = bytecode::MakeFunctionFlags::empty();
24392440

24402441
// Handle positional defaults
2441-
let defaults: Vec<_> = std::iter::empty()
2442+
let defaults: Vec<_> = core::iter::empty()
24422443
.chain(&parameters.posonlyargs)
24432444
.chain(&parameters.args)
24442445
.filter_map(|x| x.default.as_deref())
@@ -2566,7 +2567,7 @@ impl Compiler {
25662567
let mut num_annotations = 0;
25672568

25682569
// Handle parameter annotations
2569-
let parameters_iter = std::iter::empty()
2570+
let parameters_iter = core::iter::empty()
25702571
.chain(&parameters.posonlyargs)
25712572
.chain(&parameters.args)
25722573
.chain(&parameters.kwonlyargs)
@@ -4965,7 +4966,7 @@ impl Compiler {
49654966
let name = "<lambda>".to_owned();
49664967

49674968
// Prepare defaults before entering function
4968-
let defaults: Vec<_> = std::iter::empty()
4969+
let defaults: Vec<_> = core::iter::empty()
49694970
.chain(&params.posonlyargs)
49704971
.chain(&params.args)
49714972
.filter_map(|x| x.default.as_deref())

crates/codegen/src/error.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use alloc::fmt;
2+
use core::fmt::Display;
13
use rustpython_compiler_core::SourceLocation;
2-
use std::fmt::{self, Display};
34
use thiserror::Error;
45

56
#[derive(Debug)]
@@ -93,7 +94,7 @@ pub enum CodegenErrorType {
9394
NotImplementedYet, // RustPython marker for unimplemented features
9495
}
9596

96-
impl std::error::Error for CodegenErrorType {}
97+
impl core::error::Error for CodegenErrorType {}
9798

9899
impl fmt::Display for CodegenErrorType {
99100
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

crates/codegen/src/ir.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ops;
1+
use core::ops;
22

33
use crate::{IndexMap, IndexSet, error::InternalError};
44
use rustpython_compiler_core::{
@@ -198,7 +198,7 @@ impl CodeInfo {
198198
*arg = new_arg;
199199
}
200200
let (extras, lo_arg) = arg.split();
201-
locations.extend(std::iter::repeat_n(info.location, arg.instr_size()));
201+
locations.extend(core::iter::repeat_n(info.location, arg.instr_size()));
202202
instructions.extend(
203203
extras
204204
.map(|byte| CodeUnit::new(Instruction::ExtendedArg, byte))
@@ -401,7 +401,7 @@ fn stackdepth_push(
401401

402402
fn iter_blocks(blocks: &[Block]) -> impl Iterator<Item = (BlockIdx, &Block)> + '_ {
403403
let mut next = BlockIdx(0);
404-
std::iter::from_fn(move || {
404+
core::iter::from_fn(move || {
405405
if next == BlockIdx::NULL {
406406
return None;
407407
}

crates/codegen/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#[macro_use]
66
extern crate log;
77

8+
extern crate alloc;
9+
810
type IndexMap<K, V> = indexmap::IndexMap<K, V, ahash::RandomState>;
911
type IndexSet<T> = indexmap::IndexSet<T, ahash::RandomState>;
1012

crates/codegen/src/string_parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! after ruff has already successfully parsed the string literal, meaning
66
//! we don't need to do any validation or error handling.
77
8-
use std::convert::Infallible;
8+
use core::convert::Infallible;
99

1010
use ruff_python_ast::{AnyStringFlags, StringFlags};
1111
use rustpython_wtf8::{CodePoint, Wtf8, Wtf8Buf};
@@ -96,7 +96,7 @@ impl StringParser {
9696
}
9797

9898
// OK because radix_bytes is always going to be in the ASCII range.
99-
let radix_str = std::str::from_utf8(&radix_bytes[..len]).expect("ASCII bytes");
99+
let radix_str = core::str::from_utf8(&radix_bytes[..len]).expect("ASCII bytes");
100100
let value = u32::from_str_radix(radix_str, 8).unwrap();
101101
char::from_u32(value).unwrap()
102102
}

crates/codegen/src/symboltable.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::{
1111
IndexMap,
1212
error::{CodegenError, CodegenErrorType},
1313
};
14+
use alloc::{borrow::Cow, fmt};
1415
use bitflags::bitflags;
1516
use ruff_python_ast::{
1617
self as ast, Comprehension, Decorator, Expr, Identifier, ModExpression, ModModule, Parameter,
@@ -20,7 +21,6 @@ use ruff_python_ast::{
2021
};
2122
use ruff_text_size::{Ranged, TextRange};
2223
use rustpython_compiler_core::{PositionEncoding, SourceFile, SourceLocation};
23-
use std::{borrow::Cow, fmt};
2424

2525
/// Captures all symbols in the current scope, and has a list of sub-scopes in this scope.
2626
#[derive(Clone)]
@@ -215,8 +215,8 @@ impl SymbolTableError {
215215

216216
type SymbolTableResult<T = ()> = Result<T, SymbolTableError>;
217217

218-
impl std::fmt::Debug for SymbolTable {
219-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
218+
impl core::fmt::Debug for SymbolTable {
219+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
220220
write!(
221221
f,
222222
"SymbolTable({:?} symbols, {:?} sub scopes)",
@@ -261,8 +261,8 @@ fn drop_class_free(symbol_table: &mut SymbolTable) {
261261
type SymbolMap = IndexMap<String, Symbol>;
262262

263263
mod stack {
264+
use core::ptr::NonNull;
264265
use std::panic;
265-
use std::ptr::NonNull;
266266
pub struct StackStack<T> {
267267
v: Vec<NonNull<T>>,
268268
}
@@ -325,7 +325,7 @@ struct SymbolTableAnalyzer {
325325

326326
impl SymbolTableAnalyzer {
327327
fn analyze_symbol_table(&mut self, symbol_table: &mut SymbolTable) -> SymbolTableResult {
328-
let symbols = std::mem::take(&mut symbol_table.symbols);
328+
let symbols = core::mem::take(&mut symbol_table.symbols);
329329
let sub_tables = &mut *symbol_table.sub_tables;
330330

331331
let mut info = (symbols, symbol_table.typ);
@@ -689,7 +689,7 @@ impl SymbolTableBuilder {
689689
fn leave_scope(&mut self) {
690690
let mut table = self.tables.pop().unwrap();
691691
// Save the collected varnames to the symbol table
692-
table.varnames = std::mem::take(&mut self.current_varnames);
692+
table.varnames = core::mem::take(&mut self.current_varnames);
693693
self.tables.last_mut().unwrap().sub_tables.push(table);
694694
}
695695

crates/codegen/src/unparse.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
use alloc::fmt;
2+
use core::fmt::Display as _;
13
use ruff_python_ast::{
24
self as ruff, Arguments, BoolOp, Comprehension, ConversionFlag, Expr, Identifier, Operator,
35
Parameter, ParameterWithDefault, Parameters,
46
};
57
use ruff_text_size::Ranged;
68
use rustpython_compiler_core::SourceFile;
79
use rustpython_literal::escape::{AsciiEscape, UnicodeEscape};
8-
use std::fmt::{self, Display as _};
910

1011
mod precedence {
1112
macro_rules! precedence {
@@ -51,7 +52,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
5152
}
5253

5354
fn p_delim(&mut self, first: &mut bool, s: &str) -> fmt::Result {
54-
self.p_if(!std::mem::take(first), s)
55+
self.p_if(!core::mem::take(first), s)
5556
}
5657

5758
fn write_fmt(&mut self, f: fmt::Arguments<'_>) -> fmt::Result {
@@ -575,7 +576,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
575576
if conversion != ConversionFlag::None {
576577
self.p("!")?;
577578
let buf = &[conversion as u8];
578-
let c = std::str::from_utf8(buf).unwrap();
579+
let c = core::str::from_utf8(buf).unwrap();
579580
self.p(c)?;
580581
}
581582

@@ -650,7 +651,7 @@ impl fmt::Display for UnparseExpr<'_> {
650651
}
651652

652653
fn to_string_fmt(f: impl FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result) -> String {
653-
use std::cell::Cell;
654+
use core::cell::Cell;
654655
struct Fmt<F>(Cell<Option<F>>);
655656
impl<F: FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result> fmt::Display for Fmt<F> {
656657
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

crates/common/src/borrow.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ use crate::lock::{
22
MapImmutable, PyImmutableMappedMutexGuard, PyMappedMutexGuard, PyMappedRwLockReadGuard,
33
PyMappedRwLockWriteGuard, PyMutexGuard, PyRwLockReadGuard, PyRwLockWriteGuard,
44
};
5-
use std::{
6-
fmt,
7-
ops::{Deref, DerefMut},
8-
};
5+
use alloc::fmt;
6+
use core::ops::{Deref, DerefMut};
97

108
macro_rules! impl_from {
119
($lt:lifetime, $gen:ident, $t:ty, $($var:ident($from:ty),)*) => {

crates/common/src/boxvec.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
//! An unresizable vector backed by a `Box<[T]>`
33
44
#![allow(clippy::needless_lifetimes)]
5-
6-
use std::{
5+
use alloc::{fmt, slice};
6+
use core::{
77
borrow::{Borrow, BorrowMut},
8-
cmp, fmt,
8+
cmp,
99
mem::{self, MaybeUninit},
1010
ops::{Bound, Deref, DerefMut, RangeBounds},
11-
ptr, slice,
11+
ptr,
1212
};
1313

1414
pub struct BoxVec<T> {
@@ -555,7 +555,7 @@ impl<T> Extend<T> for BoxVec<T> {
555555
};
556556
let mut iter = iter.into_iter();
557557
loop {
558-
if std::ptr::eq(ptr, end_ptr) {
558+
if core::ptr::eq(ptr, end_ptr) {
559559
break;
560560
}
561561
if let Some(elt) = iter.next() {
@@ -693,7 +693,7 @@ impl<T> CapacityError<T> {
693693

694694
const CAPERROR: &str = "insufficient capacity";
695695

696-
impl<T> std::error::Error for CapacityError<T> {}
696+
impl<T> core::error::Error for CapacityError<T> {}
697697

698698
impl<T> fmt::Display for CapacityError<T> {
699699
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

0 commit comments

Comments
 (0)