From aaf65a19b0e5940dfa33667dd6d9be996d4a3982 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 30 Oct 2023 11:09:48 +0530 Subject: [PATCH] New changes due to rebase --- .../src/rules/pylint/rules/bad_open_mode.rs | 8 ++------ .../src/rules/refurb/rules/isinstance_type_none.rs | 11 +++-------- .../src/rules/refurb/rules/read_whole_file.rs | 4 ++-- .../src/expression/expr_attribute.rs | 2 +- 4 files changed, 8 insertions(+), 17 deletions(-) diff --git a/crates/ruff_linter/src/rules/pylint/rules/bad_open_mode.rs b/crates/ruff_linter/src/rules/pylint/rules/bad_open_mode.rs index 2d98d00cc5026..0750af43b59a0 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/bad_open_mode.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/bad_open_mode.rs @@ -2,7 +2,7 @@ use bitflags::bitflags; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::{self as ast, Constant, Expr}; +use ruff_python_ast::{self as ast, Expr}; use ruff_python_semantic::SemanticModel; use ruff_text_size::Ranged; @@ -59,11 +59,7 @@ pub(crate) fn bad_open_mode(checker: &mut Checker, call: &ast::ExprCall) { return; }; - let Expr::Constant(ast::ExprConstant { - value: Constant::Str(ast::StringConstant { value, .. }), - .. - }) = mode - else { + let Some(ast::ExprStringLiteral { value, .. }) = mode.as_string_literal_expr() else { return; }; diff --git a/crates/ruff_linter/src/rules/refurb/rules/isinstance_type_none.rs b/crates/ruff_linter/src/rules/refurb/rules/isinstance_type_none.rs index 8d40c3ea08a35..31822e17e8187 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/isinstance_type_none.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/isinstance_type_none.rs @@ -86,7 +86,7 @@ fn is_none(expr: &Expr) -> bool { // Ex) `None` // Note: `isinstance` only accepts `None` as a type when used with // the union operator, so we need to check if we're in a union. - Expr::Constant(ast::ExprConstant { value, .. }) if in_union_context => value.is_none(), + Expr::NoneLiteral(_) if in_union_context => true, // Ex) `type(None)` Expr::Call(ast::ExprCall { @@ -94,11 +94,7 @@ fn is_none(expr: &Expr) -> bool { }) if arguments.len() == 1 => { if let Expr::Name(ast::ExprName { id, .. }) = func.as_ref() { if id.as_str() == "type" { - if let Some(Expr::Constant(ast::ExprConstant { value, .. })) = - arguments.args.get(0) - { - return value.is_none(); - } + return matches!(arguments.args.get(0), Some(Expr::NoneLiteral(_))); } } false @@ -134,8 +130,7 @@ fn generate_replacement(name: &str, generator: Generator) -> String { let compare = ast::ExprCompare { left: Box::new(var.into()), ops: vec![ast::CmpOp::Is], - comparators: vec![ast::Expr::Constant(ast::ExprConstant { - value: ast::Constant::None, + comparators: vec![ast::Expr::NoneLiteral(ast::ExprNoneLiteral { range: TextRange::default(), })], range: TextRange::default(), diff --git a/crates/ruff_linter/src/rules/refurb/rules/read_whole_file.rs b/crates/ruff_linter/src/rules/refurb/rules/read_whole_file.rs index 49a6bce3f3246..b71306c3444ae 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/read_whole_file.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/read_whole_file.rs @@ -244,11 +244,11 @@ fn match_open_keywords( /// Match open mode to see if it is supported. fn match_open_mode(mode: &Expr) -> Option { - let ast::StringConstant { + let ast::ExprStringLiteral { value, implicit_concatenated: false, .. - } = mode.as_constant_expr()?.value.as_str()? + } = mode.as_string_literal_expr()? else { return None; }; diff --git a/crates/ruff_python_formatter/src/expression/expr_attribute.rs b/crates/ruff_python_formatter/src/expression/expr_attribute.rs index 367875467b2ff..8bf5878ff4f28 100644 --- a/crates/ruff_python_formatter/src/expression/expr_attribute.rs +++ b/crates/ruff_python_formatter/src/expression/expr_attribute.rs @@ -177,7 +177,7 @@ fn is_base_ten_number_literal(expr: &Expr, source: &str) -> bool { Some([b'0', b'x' | b'X' | b'o' | b'O' | b'b' | b'B']) ) } - _ => false, + Number::Complex { .. } => false, } } else { false