Skip to content

Commit 3e047bd

Browse files
authored
clippy enum_glob_use (RustPython#8121)
1 parent 4b0d6b6 commit 3e047bd

20 files changed

Lines changed: 319 additions & 303 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ cloned_instead_of_copied = "warn"
387387
collapsible_else_if = "warn"
388388
comparison_chain = "warn"
389389
duration_suboptimal_units = "warn"
390+
enum_glob_use = "warn"
390391
explicit_deref_methods = "warn"
391392
explicit_into_iter_loop = "warn"
392393
explicit_iter_loop = "warn"

crates/codegen/src/compile.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7005,23 +7005,42 @@ impl Compiler {
70057005

70067006
/// [CPython `compiler_addcompare`](https://github.com/python/cpython/blob/627894459a84be3488a1789919679c997056a03c/Python/compile.c#L2880-L2924)
70077007
fn compile_addcompare(&mut self, op: &ast::CmpOp) {
7008-
use bytecode::ComparisonOperator::*;
70097008
match op {
7010-
ast::CmpOp::Eq => emit!(self, Instruction::CompareOp { opname: Equal }),
7011-
ast::CmpOp::NotEq => emit!(self, Instruction::CompareOp { opname: NotEqual }),
7012-
ast::CmpOp::Lt => emit!(self, Instruction::CompareOp { opname: Less }),
7009+
ast::CmpOp::Eq => emit!(
7010+
self,
7011+
Instruction::CompareOp {
7012+
opname: ComparisonOperator::Equal
7013+
}
7014+
),
7015+
ast::CmpOp::NotEq => emit!(
7016+
self,
7017+
Instruction::CompareOp {
7018+
opname: ComparisonOperator::NotEqual
7019+
}
7020+
),
7021+
ast::CmpOp::Lt => emit!(
7022+
self,
7023+
Instruction::CompareOp {
7024+
opname: ComparisonOperator::Less
7025+
}
7026+
),
70137027
ast::CmpOp::LtE => emit!(
70147028
self,
70157029
Instruction::CompareOp {
7016-
opname: LessOrEqual
7030+
opname: ComparisonOperator::LessOrEqual
7031+
}
7032+
),
7033+
ast::CmpOp::Gt => emit!(
7034+
self,
7035+
Instruction::CompareOp {
7036+
opname: ComparisonOperator::Greater
70177037
}
70187038
),
7019-
ast::CmpOp::Gt => emit!(self, Instruction::CompareOp { opname: Greater }),
70207039
ast::CmpOp::GtE => {
70217040
emit!(
70227041
self,
70237042
Instruction::CompareOp {
7024-
opname: GreaterOrEqual
7043+
opname: ComparisonOperator::GreaterOrEqual
70257044
}
70267045
)
70277046
}

crates/codegen/src/symboltable.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,16 +2568,15 @@ impl SymbolTableBuilder {
25682568
}
25692569

25702570
fn scan_pattern(&mut self, pattern: &ast::Pattern) -> SymbolTableResult {
2571-
use ast::Pattern::*;
25722571
match pattern {
2573-
MatchValue(ast::PatternMatchValue { value, .. }) => {
2572+
ast::Pattern::MatchValue(ast::PatternMatchValue { value, .. }) => {
25742573
self.scan_expression(value, ExpressionContext::Load)?
25752574
}
2576-
MatchSingleton(_) => {}
2577-
MatchSequence(ast::PatternMatchSequence { patterns, .. }) => {
2575+
ast::Pattern::MatchSingleton(_) => {}
2576+
ast::Pattern::MatchSequence(ast::PatternMatchSequence { patterns, .. }) => {
25782577
self.scan_patterns(patterns)?
25792578
}
2580-
MatchMapping(ast::PatternMatchMapping {
2579+
ast::Pattern::MatchMapping(ast::PatternMatchMapping {
25812580
keys,
25822581
patterns,
25832582
rest,
@@ -2589,27 +2588,29 @@ impl SymbolTableBuilder {
25892588
self.register_ident(rest, SymbolUsage::Assigned)?;
25902589
}
25912590
}
2592-
MatchClass(ast::PatternMatchClass { cls, arguments, .. }) => {
2591+
ast::Pattern::MatchClass(ast::PatternMatchClass { cls, arguments, .. }) => {
25932592
self.scan_expression(cls, ExpressionContext::Load)?;
25942593
self.scan_patterns(&arguments.patterns)?;
25952594
for kw in &arguments.keywords {
25962595
self.scan_pattern(&kw.pattern)?;
25972596
}
25982597
}
2599-
MatchStar(ast::PatternMatchStar { name, .. }) => {
2598+
ast::Pattern::MatchStar(ast::PatternMatchStar { name, .. }) => {
26002599
if let Some(name) = name {
26012600
self.register_ident(name, SymbolUsage::Assigned)?;
26022601
}
26032602
}
2604-
MatchAs(ast::PatternMatchAs { pattern, name, .. }) => {
2603+
ast::Pattern::MatchAs(ast::PatternMatchAs { pattern, name, .. }) => {
26052604
if let Some(pattern) = pattern {
26062605
self.scan_pattern(pattern)?;
26072606
}
26082607
if let Some(name) = name {
26092608
self.register_ident(name, SymbolUsage::Assigned)?;
26102609
}
26112610
}
2612-
MatchOr(ast::PatternMatchOr { patterns, .. }) => self.scan_patterns(patterns)?,
2611+
ast::Pattern::MatchOr(ast::PatternMatchOr { patterns, .. }) => {
2612+
self.scan_patterns(patterns)?
2613+
}
26132614
}
26142615
Ok(())
26152616
}

crates/common/src/cformat.rs

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,17 @@ pub struct CFormatError {
3535

3636
impl fmt::Display for CFormatError {
3737
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38-
use CFormatErrorType::*;
3938
match self.typ {
40-
UnmatchedKeyParentheses => write!(f, "incomplete format key"),
41-
IncompleteFormat => write!(f, "incomplete format"),
42-
UnsupportedFormatChar(c) => write!(
39+
CFormatErrorType::UnmatchedKeyParentheses => write!(f, "incomplete format key"),
40+
CFormatErrorType::IncompleteFormat => write!(f, "incomplete format"),
41+
CFormatErrorType::UnsupportedFormatChar(c) => write!(
4342
f,
4443
"unsupported format character '{}' ({:#x}) at index {}",
4544
c,
4645
c.to_u32(),
4746
self.index
4847
),
49-
IntTooBig => write!(f, "width/precision too big"),
48+
CFormatErrorType::IntTooBig => write!(f, "width/precision too big"),
5049
_ => write!(f, "unexpected error parsing format string"),
5150
}
5251
}
@@ -78,11 +77,9 @@ pub enum CFloatType {
7877

7978
impl CFloatType {
8079
const fn case(self) -> Case {
81-
use CFloatType::*;
82-
8380
match self {
84-
ExponentLower | PointDecimalLower | GeneralLower => Case::Lower,
85-
ExponentUpper | PointDecimalUpper | GeneralUpper => Case::Upper,
81+
Self::ExponentLower | Self::PointDecimalLower | Self::GeneralLower => Case::Lower,
82+
Self::ExponentUpper | Self::PointDecimalUpper | Self::GeneralUpper => Case::Upper,
8683
}
8784
}
8885
}
@@ -443,27 +440,29 @@ impl CFormatSpec {
443440

444441
#[must_use]
445442
pub fn format_number(&self, num: &BigInt) -> String {
446-
use CNumberType::*;
447443
let CFormatType::Number(format_type) = self.format_type else {
448444
unreachable!()
449445
};
446+
450447
let magnitude = num.abs();
451448
let prefix = if self.flags.contains(CConversionFlags::ALTERNATE_FORM) {
452449
match format_type {
453-
Octal => "0o",
454-
HexLower => "0x",
455-
HexUpper => "0X",
450+
CNumberType::Octal => "0o",
451+
CNumberType::HexLower => "0x",
452+
CNumberType::HexUpper => "0X",
456453
_ => "",
457454
}
458455
} else {
459456
""
460457
};
461458

462459
let magnitude_string: String = match format_type {
463-
DecimalD | DecimalI | DecimalU => magnitude.to_str_radix(10),
464-
Octal => magnitude.to_str_radix(8),
465-
HexLower => magnitude.to_str_radix(16),
466-
HexUpper => {
460+
CNumberType::DecimalD | CNumberType::DecimalI | CNumberType::DecimalU => {
461+
magnitude.to_str_radix(10)
462+
}
463+
CNumberType::Octal => magnitude.to_str_radix(8),
464+
CNumberType::HexLower => magnitude.to_str_radix(16),
465+
CNumberType::HexUpper => {
467466
let mut result = magnitude.to_str_radix(16);
468467
result.make_ascii_uppercase();
469468
result
@@ -621,35 +620,33 @@ where
621620
C: FormatChar,
622621
I: Iterator<Item = C>,
623622
{
624-
use CFloatType::*;
625-
use CNumberType::*;
626623
let (index, c) = iter.next().ok_or_else(|| {
627624
(
628625
CFormatErrorType::IncompleteFormat,
629626
iter.peek().map_or(0, |x| x.0),
630627
)
631628
})?;
632-
let format_type = match c.to_char_lossy() {
633-
'd' => CFormatType::Number(DecimalD),
634-
'i' => CFormatType::Number(DecimalI),
635-
'u' => CFormatType::Number(DecimalU),
636-
'o' => CFormatType::Number(Octal),
637-
'x' => CFormatType::Number(HexLower),
638-
'X' => CFormatType::Number(HexUpper),
639-
'e' => CFormatType::Float(ExponentLower),
640-
'E' => CFormatType::Float(ExponentUpper),
641-
'f' => CFormatType::Float(PointDecimalLower),
642-
'F' => CFormatType::Float(PointDecimalUpper),
643-
'g' => CFormatType::Float(GeneralLower),
644-
'G' => CFormatType::Float(GeneralUpper),
629+
630+
Ok(match c.to_char_lossy() {
631+
'd' => CFormatType::Number(CNumberType::DecimalD),
632+
'i' => CFormatType::Number(CNumberType::DecimalI),
633+
'u' => CFormatType::Number(CNumberType::DecimalU),
634+
'o' => CFormatType::Number(CNumberType::Octal),
635+
'x' => CFormatType::Number(CNumberType::HexLower),
636+
'X' => CFormatType::Number(CNumberType::HexUpper),
637+
'e' => CFormatType::Float(CFloatType::ExponentLower),
638+
'E' => CFormatType::Float(CFloatType::ExponentUpper),
639+
'f' => CFormatType::Float(CFloatType::PointDecimalLower),
640+
'F' => CFormatType::Float(CFloatType::PointDecimalUpper),
641+
'g' => CFormatType::Float(CFloatType::GeneralLower),
642+
'G' => CFormatType::Float(CFloatType::GeneralUpper),
645643
'c' => CFormatType::Character(CCharacterType::Character),
646644
'r' => CFormatType::String(CFormatConversion::Repr),
647645
's' => CFormatType::String(CFormatConversion::Str),
648646
'b' => CFormatType::String(CFormatConversion::Bytes),
649647
'a' => CFormatType::String(CFormatConversion::Ascii),
650648
_ => return Err((CFormatErrorType::UnsupportedFormatChar(c.into()), index)),
651-
};
652-
Ok(format_type)
649+
})
653650
}
654651

655652
fn parse_quantity<C, I>(iter: &mut ParseIter<I>) -> Result<Option<CFormatQuantity>, ParsingError>

crates/common/src/str.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ impl core::ops::BitOr for StrKind {
2525
type Output = Self;
2626

2727
fn bitor(self, other: Self) -> Self {
28-
use StrKind::*;
2928
match (self, other) {
30-
(Wtf8, _) | (_, Wtf8) => Wtf8,
31-
(Utf8, _) | (_, Utf8) => Utf8,
32-
(Ascii, Ascii) => Ascii,
29+
(Self::Wtf8, _) | (_, Self::Wtf8) => Self::Wtf8,
30+
(Self::Utf8, _) | (_, Self::Utf8) => Self::Utf8,
31+
(Self::Ascii, Self::Ascii) => Self::Ascii,
3332
}
3433
}
3534
}

0 commit comments

Comments
 (0)