8000 Use `Self` where possible by ShaharNaveh · Pull Request #5892 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Use Self where possible #5892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/src/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl<T> Default for OncePtr<T> {
impl<T> OncePtr<T> {
#[inline]
pub fn new() -> Self {
OncePtr {
Self {
inner: Radium::new(ptr::null_mut()),
}
}
Expand Down
10 changes: 5 additions & 5 deletions common/src/boxvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ macro_rules! panic_oob {
}

impl<T> BoxVec<T> {
pub fn new(n: usize) -> BoxVec<T> {
BoxVec {
pub fn new(n: usize) -> Self {
Self {
xs: Box::new_uninit_slice(n),
len: 0,
}
Expand Down Expand Up @@ -593,7 +593,7 @@ where
T: Clone,
{
fn clone(&self) -> Self {
let mut new = BoxVec::new(self.capacity());
let mut new = Self::new(self.capacity());
new.extend(self.iter().cloned());
new
}
Expand Down Expand Up @@ -676,8 +676,8 @@ pub struct CapacityError<T = ()> {

impl<T> CapacityError<T> {
/// Create a new `CapacityError` from `element`.
pub const fn new(element: T) -> CapacityError<T> {
CapacityError { element }
pub const fn new(element: T) -> Self {
Self { element }
}

/// Extract the overflowing element
Expand Down
18 changes: 9 additions & 9 deletions common/src/cformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ pub enum CFormatType {
impl CFormatType {
pub const fn to_char(self) -> char {
match self {
CFormatType::Number(x) => x as u8 as char,
CFormatType::Float(x) => x as u8 as char,
CFormatType::Character(x) => x as u8 as char,
CFormatType::String(x) => x as u8 as char,
Self::Number(x) => x as u8 as char,
Self::Float(x) => x as u8 as char,
Self::Character(x) => x as u8 as char,
Self::String(x) => x as u8 as char,
}
}
}
Expand All @@ -119,7 +119,7 @@ pub enum CFormatPrecision {

impl From<CFormatQuantity> for CFormatPrecision {
fn from(quantity: CFormatQuantity) -> Self {
CFormatPrecision::Quantity(quantity)
Self::Quantity(quantity)
}
}

Expand Down Expand Up @@ -338,7 +338,7 @@ impl CFormatSpec {
_ => &num_chars,
};
let fill_chars_needed = width.saturating_sub(num_chars);
let fill_string: T = CFormatSpec::compute_fill_string(fill_char, fill_chars_needed);
let fill_string: T = Self::compute_fill_string(fill_char, fill_chars_needed);

if !fill_string.is_empty() {
if self.flags.contains(CConversionFlags::LEFT_ADJUST) {
Expand All @@ -361,7 +361,7 @@ impl CFormatSpec {
_ => &num_chars,
};
let fill_chars_needed = width.saturating_sub(num_chars);
let fill_string: T = CFormatSpec::compute_fill_string(fill_char, fill_chars_needed);
let fill_string: T = Self::compute_fill_string(fill_char, fill_chars_needed);

if !fill_string.is_empty() {
// Don't left-adjust if precision-filling: that will always be prepending 0s to %d
Expand Down Expand Up @@ -721,13 +721,13 @@ pub enum CFormatPart<T> {
impl<T> CFormatPart<T> {
#[inline]
pub const fn is_specifier(&self) -> bool {
matches!(self, CFormatPart::Spec { .. })
matches!(self, Self::Spec { .. })
}

#[inline]
pub const fn has_key(&self) -> bool {
match self {
CFormatPart::Spec(s) => s.mapping_key.is_some(),
Self::Spec(s) => s.mapping_key.is_some(),
_ => false,
}
}
Expand Down
76 changes: 35 additions & 41 deletions common/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ impl FormatParse for FormatConversion {
}

impl FormatConversion {
pub fn from_char(c: CodePoint) -> Option<FormatConversion> {
pub fn from_char(c: CodePoint) -> Option<Self> {
match c.to_char_lossy() {
's' => Some(FormatConversion::Str),
'r' => Some(FormatConversion::Repr),
'a' => Some(FormatConversion::Ascii),
'b' => Some(FormatConversion::Bytes),
's' => Some(Self::Str),
'r' => Some(Self::Repr),
'a' => Some(Self::Ascii),
'b' => Some(Self::Bytes),
_ => None,
}
}

fn from_string(text: &Wtf8) -> Option<FormatConversion> {
fn from_string(text: &Wtf8) -> Option<Self> {
let mut chars = text.code_points();
if chars.next()? != '!' {
return None;
}

FormatConversion::from_char(chars.next()?)
Self::from_char(chars.next()?)
}
}

Expand All @@ -67,12 +67,12 @@ pub enum FormatAlign {
}

impl FormatAlign F438 {
fn from_char(c: CodePoint) -> Option<FormatAlign> {
fn from_char(c: CodePoint) -> Option<Self> {
match c.to_char_lossy() {
'<' => Some(FormatAlign::Left),
'>' => Some(FormatAlign::Right),
'=' => Some(FormatAlign::AfterSign),
'^' => Some(FormatAlign::Center),
'<' => Some(Self::Left),
'>' => Some(Self::Right),
'=' => Some(Self::AfterSign),
'^' => Some(Self::Center),
_ => None,
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ pub enum FormatType {
}

impl From<&FormatType> for char {
fn from(from: &FormatType) -> char {
fn from(from: &FormatType) -> Self {
match from {
FormatType::String => 's',
FormatType::Binary => 'b',
Expand Down Expand Up @@ -299,7 +299,7 @@ impl FormatSpec {
align = align.or(Some(FormatAlign::AfterSign));
}

Ok(FormatSpec {
Ok(Self {
conversion,
fill,
align,
Expand Down Expand Up @@ -327,7 +327,7 @@ impl FormatSpec {
let magnitude_int_str = parts.next().unwrap().to_string();
let dec_digit_cnt = magnitude_str.len() as i32 - magnitude_int_str.len() as i32;
let int_digit_cnt = disp_digit_cnt - dec_digit_cnt;
let mut result = FormatSpec::separate_integer(magnitude_int_str, inter, sep, int_digit_cnt);
let mut result = Self::separate_integer(magnitude_int_str, inter, sep, int_digit_cnt);
if let Some(part) = parts.next() {
result.push_str(&format!(".{part}"))
}
Expand All @@ -350,11 +350,11 @@ impl FormatSpec {
// separate with 0 padding
let padding = "0".repeat(diff as usize);
let padded_num = format!("{padding}{magnitude_str}");
FormatSpec::insert_separator(padded_num, inter, sep, sep_cnt)
Self::insert_separator(padded_num, inter, sep, sep_cnt)
} else {
// separate without padding
let sep_cnt = (magnitude_len - 1) / inter;
FormatSpec::insert_separator(magnitude_str, inter, sep, sep_cnt)
Self::insert_separator(magnitude_str, inter, sep, sep_cnt)
}
}

Expand Down Expand Up @@ -412,12 +412,7 @@ impl FormatSpec {
let magnitude_len = magnitude_str.len();
let width = self.width.unwrap_or(magnitude_len) as i32 - prefix.len() as i32;
let disp_digit_cnt = cmp::max(width, magnitude_len as i32);
FormatSpec::add_magnitude_separators_for_char(
magnitude_str,
inter,
sep,
disp_digit_cnt,
)
Self::add_magnitude_separators_for_char(magnitude_str, inter, sep, disp_digit_cnt)
}
None => magnitude_str,
}
Expand Down Expand Up @@ -640,27 +635,26 @@ impl FormatSpec {
"{}{}{}",
sign_str,
magnitude_str,
FormatSpec::compute_fill_string(fill_char, fill_chars_needed)
Self::compute_fill_string(fill_char, fill_chars_needed)
),
FormatAlign::Right => format!(
"{}{}{}",
FormatSpec::compute_fill_string(fill_char, fill_chars_needed),
Self::compute_fill_string(fill_char, fill_chars_needed),
sign_str,
magnitude_str
),
FormatAlign::AfterSign => format!(
"{}{}{}",
sign_str,
FormatSpec::compute_fill_string(fill_char, fill_chars_needed),
Self::compute_fill_string(fill_char, fill_chars_needed),
magnitude_str
),
FormatAlign::Center => {
let left_fill_chars_needed = fill_chars_needed / 2;
let right_fill_chars_needed = fill_chars_needed - left_fill_chars_needed;
let left_fill_string =
FormatSpec::compute_fill_string(fill_char, left_fill_chars_needed);
let left_fill_string = Self::compute_fill_string(fill_char, left_fill_chars_needed);
let right_fill_string =
FormatSpec::compute_fill_string(fill_char, right_fill_chars_needed);
Self::compute_fill_string(fill_char, right_fill_chars_needed);
format!("{left_fill_string}{sign_str}{magnitude_str}{right_fill_string}")
}
})
Expand Down Expand Up @@ -725,7 +719,7 @@ pub enum FormatParseError {
impl FromStr for FormatSpec {
type Err = FormatSpecError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
FormatSpec::parse(s)
Self::parse(s)
}
}

Expand All @@ -739,7 +733,7 @@ pub enum FieldNamePart {
impl FieldNamePart {
fn parse_part(
chars: &mut impl PeekingNext<Item = CodePoint>,
) -> Result<Option<FieldNamePart>, FormatParseError> {
) -> Result<Option<Self>, FormatParseError> {
chars
.next()
.map(|ch| match ch.to_char_lossy() {
Expand All @@ -751,7 +745,7 @@ impl FieldNamePart {
if attribute.is_empty() {
Err(FormatParseError::EmptyAttribute)
} else {
Ok(FieldNamePart::Attribute(attribute))
Ok(Self::Attribute(attribute))
}
}
'[' => {
Expand All @@ -761,9 +755,9 @@ impl FieldNamePart {
return if index.is_empty() {
Err(FormatParseError::EmptyAttribute)
} else if let Some(index) = parse_usize(&index) {
Ok(FieldNamePart::Index(index))
Ok(Self::Index(index))
} else {
Ok(FieldNamePart::StringIndex(index))
Ok(Self::StringIndex(index))
};
}
index.push(ch);
Expand Down Expand Up @@ -794,7 +788,7 @@ fn parse_usize(s: &Wtf8) -> Option<usize> {
}

impl FieldName {
pub fn parse(text: &Wtf8) -> Result<FieldName, FormatParseError> {
pub fn parse(text: &Wtf8) -> Result<Self, FormatParseError> {
let mut chars = text.code_points().peekable();
let first: Wtf8Buf = chars
.peeking_take_while(|ch| *ch != '.' && *ch != '[')
Expand All @@ -813,7 +807,7 @@ impl FieldName {
parts.push(part)
}

Ok(FieldName { field_type, parts })
Ok(Self { field_type, parts })
}
}

Expand Down Expand Up @@ -854,7 +848,7 @@ impl FormatString {
let mut cur_text = text;
let mut result_string = Wtf8Buf::new();
while !cur_text.is_empty() {
match FormatString::parse_literal_single(cur_text) {
match Self::parse_literal_single(cur_text) {
Ok((next_char, remaining)) => {
result_string.push(next_char);
cur_text = remaining;
Expand Down Expand Up @@ -968,7 +962,7 @@ impl FormatString {
}
if let Some(pos) = end_bracket_pos {
let right = &text[pos..];
let format_part = FormatString::parse_part_in_brackets(&left)?;
let format_part = Self::parse_part_in_brackets(&left)?;
Ok((format_part, &right[1..]))
} else {
Err(FormatParseError::UnmatchedBracket)
Expand All @@ -990,14 +984,14 @@ impl<'a> FromTemplate<'a> for FormatString {
while !cur_text.is_empty() {
// Try to parse both literals and bracketed format parts until we
// run out of text
cur_text = FormatString::parse_literal(cur_text)
.or_else(|_| FormatString::parse_spec(cur_text))
cur_text = Self::parse_literal(cur_text)
.or_else(|_| Self::parse_spec(cur_text))
.map(|(part, new_text)| {
parts.push(part);
new_text
})?;
}
Ok(FormatString {
Ok(Self {
format_parts: parts,
})
}
Expand Down
8 changes: 4 additions & 4 deletions common/src/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ unsafe impl<T: Sync> Sync for Pointers<T> {}

impl<L, T> LinkedList<L, T> {
/// Creates an empty linked list.
pub const fn new() -> LinkedList<L, T> {
LinkedList {
pub const fn new() -> Self {
Self {
head: None,
// tail: None,
_marker: PhantomData,
Expand Down Expand Up @@ -323,8 +323,8 @@ where

impl<T> Pointers<T> {
/// Create a new set of empty pointers
pub fn new() -> Pointers<T> {
Pointers {
pub fn new() -> Self {
Self {
inner: UnsafeCell::new(PointersInner {
prev: None,
next: None,
Expand Down
6 changes: 3 additions & 3 deletions common/src/lock/cell_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct RawCellMutex {

unsafe impl RawMutex for RawCellMutex {
#[allow(clippy::declare_interior_mutable_const)]
const INIT: Self = RawCellMutex {
const INIT: Self = Self {
locked: Cell::new(false),
};

Expand Down Expand Up @@ -61,7 +61,7 @@ impl RawCellRwLock {

unsafe impl RawRwLock for RawCellRwLock {
#[allow(clippy::declare_interior_mutable_const)]
const INIT: Self = RawCellRwLock {
const INIT: Self = Self {
state: Cell::new(0),
};

Expand Down Expand Up @@ -203,7 +203,7 @@ fn deadlock(lock_kind: &str, ty: &str) -> ! {

pub struct SingleThreadId(());
unsafe impl GetThreadId for SingleThreadId {
const INIT: Self = SingleThreadId(());
const INIT: Self = Self(());
fn nonzero_thread_id(&self) -> NonZero<usize> {
NonZero::new(1).unwrap()
}
Expand Down
4 changes: 2 additions & 2 deletions common/src/lock/thread_mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct RawThreadMutex<R: RawMutex, G: GetThreadId> {

impl<R: RawMutex, G: GetThreadId> RawThreadMutex<R, G> {
#[allow(clippy::declare_interior_mutable_const)]
pub const INIT: Self = RawThreadMutex {
pub const INIT: Self = Self {
owner: AtomicUsize::new(0),
mutex: R::INIT,
get_thread_id: G::INIT,
Expand Down Expand Up @@ -79,7 +79,7 @@ pub struct ThreadMutex<R: RawMutex, G: GetThreadId, T: ?Sized> {

impl<R: RawMutex, G: GetThreadId, T> ThreadMutex<R, G, T> {
pub fn new(val: T) -> Self {
ThreadMutex {
Self {
raw: RawThreadMutex::INIT,
data: UnsafeCell::new(val),
}
Expand Down
Loading
Loading
0