mirror of https://github.com/astral-sh/ruff
Fix a few nursery rule violations (#2548)
This commit is contained in:
parent
38addbe50d
commit
85ca6cde49
|
|
@ -9,9 +9,7 @@ rustflags = [
|
||||||
# `https://github.com/EmbarkStudios/rust-ecosystem/issues/22#issuecomment-947011395`
|
# `https://github.com/EmbarkStudios/rust-ecosystem/issues/22#issuecomment-947011395`
|
||||||
"-Dunsafe_code",
|
"-Dunsafe_code",
|
||||||
"-Wclippy::pedantic",
|
"-Wclippy::pedantic",
|
||||||
"-Wclippy::print_stdout",
|
# Allowed pedantic lints
|
||||||
"-Wclippy::print_stderr",
|
|
||||||
"-Wclippy::dbg_macro",
|
|
||||||
"-Wclippy::char_lit_as_u8",
|
"-Wclippy::char_lit_as_u8",
|
||||||
"-Aclippy::collapsible_else_if",
|
"-Aclippy::collapsible_else_if",
|
||||||
"-Aclippy::collapsible_if",
|
"-Aclippy::collapsible_if",
|
||||||
|
|
@ -22,5 +20,9 @@ rustflags = [
|
||||||
"-Aclippy::module_name_repetitions",
|
"-Aclippy::module_name_repetitions",
|
||||||
"-Aclippy::must_use_candidate",
|
"-Aclippy::must_use_candidate",
|
||||||
"-Aclippy::similar_names",
|
"-Aclippy::similar_names",
|
||||||
"-Aclippy::too_many_lines"
|
"-Aclippy::too_many_lines",
|
||||||
|
# Disallowed restriction lints
|
||||||
|
"-Wclippy::print_stdout",
|
||||||
|
"-Wclippy::print_stderr",
|
||||||
|
"-Wclippy::dbg_macro",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -313,13 +313,13 @@ pub struct LogLevelArgs {
|
||||||
impl From<&LogLevelArgs> for LogLevel {
|
impl From<&LogLevelArgs> for LogLevel {
|
||||||
fn from(args: &LogLevelArgs) -> Self {
|
fn from(args: &LogLevelArgs) -> Self {
|
||||||
if args.silent {
|
if args.silent {
|
||||||
LogLevel::Silent
|
Self::Silent
|
||||||
} else if args.quiet {
|
} else if args.quiet {
|
||||||
LogLevel::Quiet
|
Self::Quiet
|
||||||
} else if args.verbose {
|
} else if args.verbose {
|
||||||
LogLevel::Verbose
|
Self::Verbose
|
||||||
} else {
|
} else {
|
||||||
LogLevel::Default
|
Self::Default
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ pub struct Printer<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Printer<'a> {
|
impl<'a> Printer<'a> {
|
||||||
pub fn new(
|
pub const fn new(
|
||||||
format: &'a SerializationFormat,
|
format: &'a SerializationFormat,
|
||||||
log_level: &'a LogLevel,
|
log_level: &'a LogLevel,
|
||||||
autofix: &'a fix::FixMode,
|
autofix: &'a fix::FixMode,
|
||||||
|
|
|
||||||
|
|
@ -176,7 +176,7 @@ impl Parse for FieldAttributes {
|
||||||
input.parse::<Comma>()?;
|
input.parse::<Comma>()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(FieldAttributes {
|
Ok(Self {
|
||||||
default,
|
default,
|
||||||
value_type,
|
value_type,
|
||||||
example: textwrap::dedent(&example).trim_matches('\n').to_string(),
|
example: textwrap::dedent(&example).trim_matches('\n').to_string(),
|
||||||
|
|
|
||||||
|
|
@ -148,6 +148,6 @@ impl Parse for Mapping {
|
||||||
let _: Token![,] = input.parse()?;
|
let _: Token![,] = input.parse()?;
|
||||||
entries.push((code, path, name));
|
entries.push((code, path, name));
|
||||||
}
|
}
|
||||||
Ok(Mapping { entries })
|
Ok(Self { entries })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,11 +30,11 @@ impl<'a> From<&'a Expr> for HashableExpr<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> HashableExpr<'a> {
|
impl<'a> HashableExpr<'a> {
|
||||||
pub(crate) fn from_expr(expr: &'a Expr) -> Self {
|
pub(crate) const fn from_expr(expr: &'a Expr) -> Self {
|
||||||
Self(expr)
|
Self(expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn as_expr(&self) -> &'a Expr {
|
pub(crate) const fn as_expr(&self) -> &'a Expr {
|
||||||
self.0
|
self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -445,7 +445,7 @@ pub fn is_assignment_to_a_dunder(stmt: &Stmt) -> bool {
|
||||||
|
|
||||||
/// Return `true` if the [`Expr`] is a singleton (`None`, `True`, `False`, or
|
/// Return `true` if the [`Expr`] is a singleton (`None`, `True`, `False`, or
|
||||||
/// `...`).
|
/// `...`).
|
||||||
pub fn is_singleton(expr: &Expr) -> bool {
|
pub const fn is_singleton(expr: &Expr) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
expr.node,
|
expr.node,
|
||||||
ExprKind::Constant {
|
ExprKind::Constant {
|
||||||
|
|
@ -479,7 +479,7 @@ pub fn find_keyword<'a>(keywords: &'a [Keyword], keyword_name: &str) -> Option<&
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `true` if an [`Expr`] is `None`.
|
/// Return `true` if an [`Expr`] is `None`.
|
||||||
pub fn is_const_none(expr: &Expr) -> bool {
|
pub const fn is_const_none(expr: &Expr) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
&expr.node,
|
&expr.node,
|
||||||
ExprKind::Constant {
|
ExprKind::Constant {
|
||||||
|
|
@ -490,7 +490,7 @@ pub fn is_const_none(expr: &Expr) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `true` if an [`Expr`] is `True`.
|
/// Return `true` if an [`Expr`] is `True`.
|
||||||
pub fn is_const_true(expr: &Expr) -> bool {
|
pub const fn is_const_true(expr: &Expr) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
&expr.node,
|
&expr.node,
|
||||||
ExprKind::Constant {
|
ExprKind::Constant {
|
||||||
|
|
@ -753,11 +753,10 @@ pub fn binding_range(binding: &Binding, locator: &Locator) -> Range {
|
||||||
binding.kind,
|
binding.kind,
|
||||||
BindingKind::ClassDefinition | BindingKind::FunctionDefinition
|
BindingKind::ClassDefinition | BindingKind::FunctionDefinition
|
||||||
) {
|
) {
|
||||||
if let Some(source) = &binding.source {
|
binding
|
||||||
identifier_range(source, locator)
|
.source
|
||||||
} else {
|
.as_ref()
|
||||||
binding.range
|
.map_or(binding.range, |source| identifier_range(source, locator))
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
binding.range
|
binding.range
|
||||||
}
|
}
|
||||||
|
|
@ -959,7 +958,7 @@ pub fn followed_by_multi_statement_line(stmt: &Stmt, locator: &Locator) -> bool
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `true` if a `Stmt` is a docstring.
|
/// Return `true` if a `Stmt` is a docstring.
|
||||||
pub fn is_docstring_stmt(stmt: &Stmt) -> bool {
|
pub const fn is_docstring_stmt(stmt: &Stmt) -> bool {
|
||||||
if let StmtKind::Expr { value } = &stmt.node {
|
if let StmtKind::Expr { value } = &stmt.node {
|
||||||
matches!(
|
matches!(
|
||||||
value.node,
|
value.node,
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ pub struct Range {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Range {
|
impl Range {
|
||||||
pub fn new(location: Location, end_location: Location) -> Self {
|
pub const fn new(location: Location, end_location: Location) -> Self {
|
||||||
Self {
|
Self {
|
||||||
location,
|
location,
|
||||||
end_location,
|
end_location,
|
||||||
|
|
@ -179,13 +179,13 @@ impl<'a> Binding<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn used(&self) -> bool {
|
pub const fn used(&self) -> bool {
|
||||||
self.runtime_usage.is_some()
|
self.runtime_usage.is_some()
|
||||||
|| self.synthetic_usage.is_some()
|
|| self.synthetic_usage.is_some()
|
||||||
|| self.typing_usage.is_some()
|
|| self.typing_usage.is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_definition(&self) -> bool {
|
pub const fn is_definition(&self) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
self.kind,
|
self.kind,
|
||||||
BindingKind::ClassDefinition
|
BindingKind::ClassDefinition
|
||||||
|
|
|
||||||
|
|
@ -3785,11 +3785,11 @@ impl<'a> Checker<'a> {
|
||||||
.map(|index| &self.scopes[*index])
|
.map(|index| &self.scopes[*index])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn in_exception_handler(&self) -> bool {
|
pub const fn in_exception_handler(&self) -> bool {
|
||||||
self.in_exception_handler
|
self.in_exception_handler
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn execution_context(&self) -> ExecutionContext {
|
pub const fn execution_context(&self) -> ExecutionContext {
|
||||||
if self.in_type_checking_block
|
if self.in_type_checking_block
|
||||||
|| self.in_annotation
|
|| self.in_annotation
|
||||||
|| self.in_deferred_string_type_definition
|
|| self.in_deferred_string_type_definition
|
||||||
|
|
|
||||||
|
|
@ -32,11 +32,7 @@ pub fn compose_module_path(module: &NameOrAttribute) -> String {
|
||||||
NameOrAttribute::A(attr) => {
|
NameOrAttribute::A(attr) => {
|
||||||
let name = attr.attr.value;
|
let name = attr.attr.value;
|
||||||
let prefix = compose_call_path(&attr.value);
|
let prefix = compose_call_path(&attr.value);
|
||||||
if let Some(prefix) = prefix {
|
prefix.map_or_else(|| name.to_string(), |prefix| format!("{prefix}.{name}"))
|
||||||
format!("{prefix}.{name}")
|
|
||||||
} else {
|
|
||||||
name.to_string()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,9 +22,9 @@ impl Flags {
|
||||||
.iter_enabled()
|
.iter_enabled()
|
||||||
.any(|rule_code| matches!(rule_code.lint_source(), LintSource::Imports))
|
.any(|rule_code| matches!(rule_code.lint_source(), LintSource::Imports))
|
||||||
{
|
{
|
||||||
Flags::NOQA | Flags::ISORT
|
Self::NOQA | Self::ISORT
|
||||||
} else {
|
} else {
|
||||||
Flags::NOQA
|
Self::NOQA
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -84,11 +84,9 @@ pub(crate) fn section_contexts<'a>(
|
||||||
section_name: context.section_name,
|
section_name: context.section_name,
|
||||||
previous_line: context.previous_line,
|
previous_line: context.previous_line,
|
||||||
line: context.line,
|
line: context.line,
|
||||||
following_lines: if let Some(end) = end {
|
following_lines: end.map_or(context.following_lines, |end| {
|
||||||
&lines[context.original_index + 1..end]
|
&lines[context.original_index + 1..end]
|
||||||
} else {
|
}),
|
||||||
context.following_lines
|
|
||||||
},
|
|
||||||
original_index: context.original_index,
|
original_index: context.original_index,
|
||||||
is_last_section: end.is_none(),
|
is_last_section: end.is_none(),
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,9 @@ pub enum FixMode {
|
||||||
impl From<bool> for FixMode {
|
impl From<bool> for FixMode {
|
||||||
fn from(value: bool) -> Self {
|
fn from(value: bool) -> Self {
|
||||||
if value {
|
if value {
|
||||||
FixMode::Apply
|
Self::Apply
|
||||||
} else {
|
} else {
|
||||||
FixMode::None
|
Self::None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -27,7 +27,7 @@ pub struct Fix {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Fix {
|
impl Fix {
|
||||||
pub fn deletion(start: Location, end: Location) -> Self {
|
pub const fn deletion(start: Location, end: Location) -> Self {
|
||||||
Self {
|
Self {
|
||||||
content: String::new(),
|
content: String::new(),
|
||||||
location: start,
|
location: start,
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@ struct State {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
fn new() -> Self {
|
const fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
seen_sep: true,
|
seen_sep: true,
|
||||||
seen_colon: false,
|
seen_colon: false,
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ impl Default for StateMachine {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StateMachine {
|
impl StateMachine {
|
||||||
pub fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
state: State::ExpectModuleDocstring,
|
state: State::ExpectModuleDocstring,
|
||||||
bracket_count: 0,
|
bracket_count: 0,
|
||||||
|
|
|
||||||
|
|
@ -34,8 +34,8 @@ pub struct LinterResult<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> LinterResult<T> {
|
impl<T> LinterResult<T> {
|
||||||
fn new(data: T, error: Option<ParseError>) -> Self {
|
const fn new(data: T, error: Option<ParseError>) -> Self {
|
||||||
LinterResult { data, error }
|
Self { data, error }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map<U, F: FnOnce(T) -> U>(self, f: F) -> LinterResult<U> {
|
fn map<U, F: FnOnce(T) -> U>(self, f: F) -> LinterResult<U> {
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ pub enum LogLevel {
|
||||||
|
|
||||||
impl LogLevel {
|
impl LogLevel {
|
||||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||||
fn level_filter(&self) -> log::LevelFilter {
|
const fn level_filter(&self) -> log::LevelFilter {
|
||||||
match self {
|
match self {
|
||||||
LogLevel::Default => log::LevelFilter::Info,
|
LogLevel::Default => log::LevelFilter::Info,
|
||||||
LogLevel::Verbose => log::LevelFilter::Debug,
|
LogLevel::Verbose => log::LevelFilter::Debug,
|
||||||
|
|
|
||||||
|
|
@ -648,7 +648,7 @@ pub trait RuleNamespace: Sized {
|
||||||
pub struct UpstreamCategory(pub RuleCodePrefix, pub &'static str);
|
pub struct UpstreamCategory(pub RuleCodePrefix, pub &'static str);
|
||||||
|
|
||||||
impl Linter {
|
impl Linter {
|
||||||
pub fn upstream_categories(&self) -> Option<&'static [UpstreamCategory]> {
|
pub const fn upstream_categories(&self) -> Option<&'static [UpstreamCategory]> {
|
||||||
match self {
|
match self {
|
||||||
Linter::Pycodestyle => Some(&[
|
Linter::Pycodestyle => Some(&[
|
||||||
UpstreamCategory(RuleCodePrefix::E, "Error"),
|
UpstreamCategory(RuleCodePrefix::E, "Error"),
|
||||||
|
|
@ -678,7 +678,7 @@ pub enum LintSource {
|
||||||
impl Rule {
|
impl Rule {
|
||||||
/// The source for the diagnostic (either the AST, the filesystem, or the
|
/// The source for the diagnostic (either the AST, the filesystem, or the
|
||||||
/// physical lines).
|
/// physical lines).
|
||||||
pub fn lint_source(&self) -> &'static LintSource {
|
pub const fn lint_source(&self) -> &'static LintSource {
|
||||||
match self {
|
match self {
|
||||||
Rule::UnusedNOQA => &LintSource::NoQa,
|
Rule::UnusedNOQA => &LintSource::NoQa,
|
||||||
Rule::BlanketNOQA
|
Rule::BlanketNOQA
|
||||||
|
|
|
||||||
|
|
@ -76,11 +76,7 @@ fn get_int_value(expr: &Expr) -> Option<u16> {
|
||||||
..
|
..
|
||||||
} => value.to_u16(),
|
} => value.to_u16(),
|
||||||
ExprKind::Attribute { .. } => {
|
ExprKind::Attribute { .. } => {
|
||||||
if let Some(path) = compose_call_path(expr) {
|
compose_call_path(expr).and_then(|path| PYSTAT_MAPPING.get(path.as_str()).copied())
|
||||||
PYSTAT_MAPPING.get(path.as_str()).copied()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ExprKind::BinOp { left, op, right } => {
|
ExprKind::BinOp { left, op, right } => {
|
||||||
if let (Some(left_value), Some(right_value)) =
|
if let (Some(left_value), Some(right_value)) =
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@ fn allow_boolean_trap(func: &Expr) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_boolean_arg(arg: &Expr) -> bool {
|
const fn is_boolean_arg(arg: &Expr) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
&arg.node,
|
&arg.node,
|
||||||
ExprKind::Constant {
|
ExprKind::Constant {
|
||||||
|
|
|
||||||
|
|
@ -37,14 +37,14 @@ struct Token<'tok> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tok> Token<'tok> {
|
impl<'tok> Token<'tok> {
|
||||||
fn irrelevant() -> Token<'static> {
|
const fn irrelevant() -> Token<'static> {
|
||||||
Token {
|
Token {
|
||||||
type_: TokenType::Irrelevant,
|
type_: TokenType::Irrelevant,
|
||||||
spanned: None,
|
spanned: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_spanned(spanned: &'tok Spanned) -> Token<'tok> {
|
const fn from_spanned(spanned: &'tok Spanned) -> Token<'tok> {
|
||||||
let type_ = match &spanned.1 {
|
let type_ = match &spanned.1 {
|
||||||
Tok::NonLogicalNewline => TokenType::NonLogicalNewline,
|
Tok::NonLogicalNewline => TokenType::NonLogicalNewline,
|
||||||
Tok::Newline => TokenType::Newline,
|
Tok::Newline => TokenType::Newline,
|
||||||
|
|
@ -97,8 +97,8 @@ struct Context {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Context {
|
impl Context {
|
||||||
fn new(type_: ContextType) -> Self {
|
const fn new(type_: ContextType) -> Self {
|
||||||
Context {
|
Self {
|
||||||
type_,
|
type_,
|
||||||
num_commas: 0,
|
num_commas: 0,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -77,7 +77,7 @@ struct ExceptionHandlerVisitor<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ExceptionHandlerVisitor<'a> {
|
impl<'a> ExceptionHandlerVisitor<'a> {
|
||||||
fn new(exception_name: &'a str) -> Self {
|
const fn new(exception_name: &'a str) -> Self {
|
||||||
Self {
|
Self {
|
||||||
exception_name,
|
exception_name,
|
||||||
current_assert: None,
|
current_assert: None,
|
||||||
|
|
@ -123,7 +123,7 @@ where
|
||||||
/// Check if the test expression is a composite condition.
|
/// Check if the test expression is a composite condition.
|
||||||
/// For example, `a and b` or `not (a or b)`. The latter is equivalent
|
/// For example, `a and b` or `not (a or b)`. The latter is equivalent
|
||||||
/// to `not a and not b` by De Morgan's laws.
|
/// to `not a and not b` by De Morgan's laws.
|
||||||
fn is_composite_condition(test: &Expr) -> bool {
|
const fn is_composite_condition(test: &Expr) -> bool {
|
||||||
match &test.node {
|
match &test.node {
|
||||||
ExprKind::BoolOp {
|
ExprKind::BoolOp {
|
||||||
op: Boolop::And, ..
|
op: Boolop::And, ..
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ fn is_pytest_raises(checker: &Checker, func: &Expr) -> bool {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_non_trivial_with_body(body: &[Stmt]) -> bool {
|
const fn is_non_trivial_with_body(body: &[Stmt]) -> bool {
|
||||||
if body.len() > 1 {
|
if body.len() > 1 {
|
||||||
true
|
true
|
||||||
} else if let Some(first_body_stmt) = body.first() {
|
} else if let Some(first_body_stmt) = body.first() {
|
||||||
|
|
|
||||||
|
|
@ -98,35 +98,35 @@ impl AlwaysAutofixableViolation for AvoidQuoteEscape {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn good_single(quote: &Quote) -> char {
|
const fn good_single(quote: &Quote) -> char {
|
||||||
match quote {
|
match quote {
|
||||||
Quote::Single => '\'',
|
Quote::Single => '\'',
|
||||||
Quote::Double => '"',
|
Quote::Double => '"',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bad_single(quote: &Quote) -> char {
|
const fn bad_single(quote: &Quote) -> char {
|
||||||
match quote {
|
match quote {
|
||||||
Quote::Double => '\'',
|
Quote::Double => '\'',
|
||||||
Quote::Single => '"',
|
Quote::Single => '"',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn good_multiline(quote: &Quote) -> &str {
|
const fn good_multiline(quote: &Quote) -> &str {
|
||||||
match quote {
|
match quote {
|
||||||
Quote::Single => "'''",
|
Quote::Single => "'''",
|
||||||
Quote::Double => "\"\"\"",
|
Quote::Double => "\"\"\"",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn good_multiline_ending(quote: &Quote) -> &str {
|
const fn good_multiline_ending(quote: &Quote) -> &str {
|
||||||
match quote {
|
match quote {
|
||||||
Quote::Single => "'\"\"\"",
|
Quote::Single => "'\"\"\"",
|
||||||
Quote::Double => "\"'''",
|
Quote::Double => "\"'''",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn good_docstring(quote: &Quote) -> &str {
|
const fn good_docstring(quote: &Quote) -> &str {
|
||||||
match quote {
|
match quote {
|
||||||
Quote::Single => "'",
|
Quote::Single => "'",
|
||||||
Quote::Double => "\"",
|
Quote::Double => "\"",
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ pub fn is_type_checking_block(checker: &Checker, test: &Expr) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_valid_runtime_import(binding: &Binding) -> bool {
|
pub const fn is_valid_runtime_import(binding: &Binding) -> bool {
|
||||||
if matches!(
|
if matches!(
|
||||||
binding.kind,
|
binding.kind,
|
||||||
BindingKind::Importation(..)
|
BindingKind::Importation(..)
|
||||||
|
|
|
||||||
|
|
@ -13,21 +13,21 @@ pub enum Argumentable {
|
||||||
impl Argumentable {
|
impl Argumentable {
|
||||||
pub fn check_for(&self, name: String) -> DiagnosticKind {
|
pub fn check_for(&self, name: String) -> DiagnosticKind {
|
||||||
match self {
|
match self {
|
||||||
Argumentable::Function => rules::UnusedFunctionArgument { name }.into(),
|
Self::Function => rules::UnusedFunctionArgument { name }.into(),
|
||||||
Argumentable::Method => rules::UnusedMethodArgument { name }.into(),
|
Self::Method => rules::UnusedMethodArgument { name }.into(),
|
||||||
Argumentable::ClassMethod => rules::UnusedClassMethodArgument { name }.into(),
|
Self::ClassMethod => rules::UnusedClassMethodArgument { name }.into(),
|
||||||
Argumentable::StaticMethod => rules::UnusedStaticMethodArgument { name }.into(),
|
Self::StaticMethod => rules::UnusedStaticMethodArgument { name }.into(),
|
||||||
Argumentable::Lambda => rules::UnusedLambdaArgument { name }.into(),
|
Self::Lambda => rules::UnusedLambdaArgument { name }.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rule_code(&self) -> &Rule {
|
pub const fn rule_code(&self) -> &Rule {
|
||||||
match self {
|
match self {
|
||||||
Argumentable::Function => &Rule::UnusedFunctionArgument,
|
Self::Function => &Rule::UnusedFunctionArgument,
|
||||||
Argumentable::Method => &Rule::UnusedMethodArgument,
|
Self::Method => &Rule::UnusedMethodArgument,
|
||||||
Argumentable::ClassMethod => &Rule::UnusedClassMethodArgument,
|
Self::ClassMethod => &Rule::UnusedClassMethodArgument,
|
||||||
Argumentable::StaticMethod => &Rule::UnusedStaticMethodArgument,
|
Self::StaticMethod => &Rule::UnusedStaticMethodArgument,
|
||||||
Argumentable::Lambda => &Rule::UnusedLambdaArgument,
|
Self::Lambda => &Rule::UnusedLambdaArgument,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use rustpython_ast::{Expr, ExprKind};
|
||||||
|
|
||||||
/// Return `true` if an `Expr` _could_ be a `DataFrame`. This rules out
|
/// Return `true` if an `Expr` _could_ be a `DataFrame`. This rules out
|
||||||
/// obviously-wrong cases, like constants and literals.
|
/// obviously-wrong cases, like constants and literals.
|
||||||
pub fn is_dataframe_candidate(expr: &Expr) -> bool {
|
pub const fn is_dataframe_candidate(expr: &Expr) -> bool {
|
||||||
!matches!(
|
!matches!(
|
||||||
expr.node,
|
expr.node,
|
||||||
ExprKind::Constant { .. }
|
ExprKind::Constant { .. }
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ pub enum Convention {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Convention {
|
impl Convention {
|
||||||
pub fn rules_to_be_ignored(self) -> &'static [Rule] {
|
pub const fn rules_to_be_ignored(self) -> &'static [Rule] {
|
||||||
match self {
|
match self {
|
||||||
Convention::Google => &[
|
Convention::Google => &[
|
||||||
Rule::OneBlankLineBeforeClass,
|
Rule::OneBlankLineBeforeClass,
|
||||||
|
|
|
||||||
|
|
@ -225,7 +225,7 @@ struct ImportReplacer<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ImportReplacer<'a> {
|
impl<'a> ImportReplacer<'a> {
|
||||||
fn new(
|
const fn new(
|
||||||
stmt: &'a Stmt,
|
stmt: &'a Stmt,
|
||||||
module: &'a str,
|
module: &'a str,
|
||||||
members: &'a [AliasData],
|
members: &'a [AliasData],
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ struct BlockMetadata {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BlockMetadata {
|
impl BlockMetadata {
|
||||||
fn new(starter: Tok, elif: Option<Location>, else_: Option<Location>) -> Self {
|
const fn new(starter: Tok, elif: Option<Location>, else_: Option<Location>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
starter,
|
starter,
|
||||||
elif,
|
elif,
|
||||||
|
|
|
||||||
|
|
@ -63,13 +63,13 @@ impl FromStr for OpenMode {
|
||||||
|
|
||||||
fn from_str(string: &str) -> Result<Self, Self::Err> {
|
fn from_str(string: &str) -> Result<Self, Self::Err> {
|
||||||
match string {
|
match string {
|
||||||
"U" => Ok(OpenMode::U),
|
"U" => Ok(Self::U),
|
||||||
"Ur" => Ok(OpenMode::Ur),
|
"Ur" => Ok(Self::Ur),
|
||||||
"Ub" => Ok(OpenMode::Ub),
|
"Ub" => Ok(Self::Ub),
|
||||||
"rUb" => Ok(OpenMode::RUb),
|
"rUb" => Ok(Self::RUb),
|
||||||
"r" => Ok(OpenMode::R),
|
"r" => Ok(Self::R),
|
||||||
"rt" => Ok(OpenMode::Rt),
|
"rt" => Ok(Self::Rt),
|
||||||
"wt" => Ok(OpenMode::Wt),
|
"wt" => Ok(Self::Wt),
|
||||||
_ => Err(anyhow!("Unknown open mode: {}", string)),
|
_ => Err(anyhow!("Unknown open mode: {}", string)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -78,13 +78,13 @@ impl FromStr for OpenMode {
|
||||||
impl OpenMode {
|
impl OpenMode {
|
||||||
fn replacement_value(&self) -> Option<String> {
|
fn replacement_value(&self) -> Option<String> {
|
||||||
match *self {
|
match *self {
|
||||||
OpenMode::U => None,
|
Self::U => None,
|
||||||
OpenMode::Ur => None,
|
Self::Ur => None,
|
||||||
OpenMode::Ub => Some(String::from("\"rb\"")),
|
Self::Ub => Some(String::from("\"rb\"")),
|
||||||
OpenMode::RUb => Some(String::from("\"rb\"")),
|
Self::RUb => Some(String::from("\"rb\"")),
|
||||||
OpenMode::R => None,
|
Self::R => None,
|
||||||
OpenMode::Rt => None,
|
Self::Rt => None,
|
||||||
OpenMode::Wt => Some(String::from("\"w\"")),
|
Self::Wt => Some(String::from("\"w\"")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,26 +12,26 @@ pub enum Primitive {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Primitive {
|
impl Primitive {
|
||||||
pub fn from_constant(constant: &Constant) -> Option<Self> {
|
pub const fn from_constant(constant: &Constant) -> Option<Self> {
|
||||||
match constant {
|
match constant {
|
||||||
Constant::Bool(_) => Some(Primitive::Bool),
|
Constant::Bool(_) => Some(Self::Bool),
|
||||||
Constant::Str(_) => Some(Primitive::Str),
|
Constant::Str(_) => Some(Self::Str),
|
||||||
Constant::Bytes(_) => Some(Primitive::Bytes),
|
Constant::Bytes(_) => Some(Self::Bytes),
|
||||||
Constant::Int(_) => Some(Primitive::Int),
|
Constant::Int(_) => Some(Self::Int),
|
||||||
Constant::Float(_) => Some(Primitive::Float),
|
Constant::Float(_) => Some(Self::Float),
|
||||||
Constant::Complex { .. } => Some(Primitive::Complex),
|
Constant::Complex { .. } => Some(Self::Complex),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn builtin(self) -> String {
|
pub fn builtin(self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Primitive::Bool => "bool".to_string(),
|
Self::Bool => "bool".to_string(),
|
||||||
Primitive::Str => "str".to_string(),
|
Self::Str => "str".to_string(),
|
||||||
Primitive::Bytes => "bytes".to_string(),
|
Self::Bytes => "bytes".to_string(),
|
||||||
Primitive::Int => "int".to_string(),
|
Self::Int => "int".to_string(),
|
||||||
Primitive::Float => "float".to_string(),
|
Self::Float => "float".to_string(),
|
||||||
Primitive::Complex => "complex".to_string(),
|
Self::Complex => "complex".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,7 @@ fn check_type_check_test(checker: &mut Checker, test: &Expr) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the [`Expr`] representing the name of the exception.
|
/// Returns the [`Expr`] representing the name of the exception.
|
||||||
fn match_name(exc: &Expr) -> Option<&Expr> {
|
const fn match_name(exc: &Expr) -> Option<&Expr> {
|
||||||
match &exc.node {
|
match &exc.node {
|
||||||
ExprKind::Name { .. } => Some(exc),
|
ExprKind::Name { .. } => Some(exc),
|
||||||
ExprKind::Call { func, .. } => {
|
ExprKind::Call { func, .. } => {
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ pub struct Configuration {
|
||||||
|
|
||||||
impl Configuration {
|
impl Configuration {
|
||||||
pub fn from_options(options: Options, project_root: &Path) -> Result<Self> {
|
pub fn from_options(options: Options, project_root: &Path) -> Result<Self> {
|
||||||
Ok(Configuration {
|
Ok(Self {
|
||||||
rule_selections: vec![RuleSelection {
|
rule_selections: vec![RuleSelection {
|
||||||
select: options.select,
|
select: options.select,
|
||||||
ignore: options
|
ignore: options
|
||||||
|
|
@ -197,7 +197,7 @@ impl Configuration {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn combine(self, config: Configuration) -> Self {
|
pub fn combine(self, config: Self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
rule_selections: config
|
rule_selections: config
|
||||||
.rule_selections
|
.rule_selections
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,9 @@ pub enum Autofix {
|
||||||
impl From<bool> for Autofix {
|
impl From<bool> for Autofix {
|
||||||
fn from(value: bool) -> Self {
|
fn from(value: bool) -> Self {
|
||||||
if value {
|
if value {
|
||||||
Autofix::Enabled
|
Self::Enabled
|
||||||
} else {
|
} else {
|
||||||
Autofix::Disabled
|
Self::Disabled
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -19,8 +19,8 @@ impl From<bool> for Autofix {
|
||||||
impl From<fix::FixMode> for Autofix {
|
impl From<fix::FixMode> for Autofix {
|
||||||
fn from(value: fix::FixMode) -> Self {
|
fn from(value: fix::FixMode) -> Self {
|
||||||
match value {
|
match value {
|
||||||
fix::FixMode::Generate | fix::FixMode::Diff | fix::FixMode::Apply => Autofix::Enabled,
|
fix::FixMode::Generate | fix::FixMode::Diff | fix::FixMode::Apply => Self::Enabled,
|
||||||
fix::FixMode::None => Autofix::Disabled,
|
fix::FixMode::None => Self::Disabled,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -34,9 +34,9 @@ pub enum Noqa {
|
||||||
impl From<bool> for Noqa {
|
impl From<bool> for Noqa {
|
||||||
fn from(value: bool) -> Self {
|
fn from(value: bool) -> Self {
|
||||||
if value {
|
if value {
|
||||||
Noqa::Enabled
|
Self::Enabled
|
||||||
} else {
|
} else {
|
||||||
Noqa::Disabled
|
Self::Disabled
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -50,9 +50,9 @@ pub enum Cache {
|
||||||
impl From<bool> for Cache {
|
impl From<bool> for Cache {
|
||||||
fn from(value: bool) -> Self {
|
fn from(value: bool) -> Self {
|
||||||
if value {
|
if value {
|
||||||
Cache::Enabled
|
Self::Enabled
|
||||||
} else {
|
} else {
|
||||||
Cache::Disabled
|
Self::Disabled
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -225,7 +225,7 @@ impl Settings {
|
||||||
pub fn for_rule(rule_code: Rule) -> Self {
|
pub fn for_rule(rule_code: Rule) -> Self {
|
||||||
Self {
|
Self {
|
||||||
rules: [rule_code].into(),
|
rules: [rule_code].into(),
|
||||||
..Settings::default()
|
..Self::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -233,7 +233,7 @@ impl Settings {
|
||||||
pub fn for_rules(rules: impl IntoIterator<Item = Rule>) -> Self {
|
pub fn for_rules(rules: impl IntoIterator<Item = Rule>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
rules: rules.into(),
|
rules: rules.into(),
|
||||||
..Settings::default()
|
..Self::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -361,7 +361,7 @@ impl From<&Configuration> for RuleTable {
|
||||||
crate::warn_user!("`{from}` has been remapped to `{}`.", target.as_ref());
|
crate::warn_user!("`{from}` has been remapped to `{}`.", target.as_ref());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut rules = RuleTable::empty();
|
let mut rules = Self::empty();
|
||||||
|
|
||||||
for rule in select_set {
|
for rule in select_set {
|
||||||
let fix = fixable_set.contains(&rule);
|
let fix = fixable_set.contains(&rule);
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ pub struct Pyproject {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pyproject {
|
impl Pyproject {
|
||||||
pub fn new(options: Options) -> Self {
|
pub const fn new(options: Options) -> Self {
|
||||||
Self {
|
Self {
|
||||||
tool: Some(Tools {
|
tool: Some(Tools {
|
||||||
ruff: Some(options),
|
ruff: Some(options),
|
||||||
|
|
|
||||||
|
|
@ -37,26 +37,26 @@ impl FromStr for PythonVersion {
|
||||||
"Specified a version below the minimum supported Python version. Defaulting \
|
"Specified a version below the minimum supported Python version. Defaulting \
|
||||||
to Python 3.7."
|
to Python 3.7."
|
||||||
);
|
);
|
||||||
Ok(PythonVersion::Py37)
|
Ok(Self::Py37)
|
||||||
}
|
}
|
||||||
"py37" => Ok(PythonVersion::Py37),
|
"py37" => Ok(Self::Py37),
|
||||||
"py38" => Ok(PythonVersion::Py38),
|
"py38" => Ok(Self::Py38),
|
||||||
"py39" => Ok(PythonVersion::Py39),
|
"py39" => Ok(Self::Py39),
|
||||||
"py310" => Ok(PythonVersion::Py310),
|
"py310" => Ok(Self::Py310),
|
||||||
"py311" => Ok(PythonVersion::Py311),
|
"py311" => Ok(Self::Py311),
|
||||||
_ => Err(anyhow!("Unknown version: {string} (try: \"py37\")")),
|
_ => Err(anyhow!("Unknown version: {string} (try: \"py37\")")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PythonVersion {
|
impl PythonVersion {
|
||||||
pub fn as_tuple(&self) -> (u32, u32) {
|
pub const fn as_tuple(&self) -> (u32, u32) {
|
||||||
match self {
|
match self {
|
||||||
PythonVersion::Py37 => (3, 7),
|
Self::Py37 => (3, 7),
|
||||||
PythonVersion::Py38 => (3, 8),
|
Self::Py38 => (3, 8),
|
||||||
PythonVersion::Py39 => (3, 9),
|
Self::Py39 => (3, 9),
|
||||||
PythonVersion::Py310 => (3, 10),
|
Self::Py310 => (3, 10),
|
||||||
PythonVersion::Py311 => (3, 11),
|
Self::Py311 => (3, 11),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,8 +57,12 @@ impl<'a> From<&'a Stylist<'a>> for Generator<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Generator<'a> {
|
impl<'a> Generator<'a> {
|
||||||
pub fn new(indent: &'a Indentation, quote: &'a Quote, line_ending: &'a LineEnding) -> Self {
|
pub const fn new(
|
||||||
Generator {
|
indent: &'a Indentation,
|
||||||
|
quote: &'a Quote,
|
||||||
|
line_ending: &'a LineEnding,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
// Style preferences.
|
// Style preferences.
|
||||||
indent,
|
indent,
|
||||||
quote,
|
quote,
|
||||||
|
|
|
||||||
|
|
@ -93,8 +93,8 @@ fn truncate(location: Location, index: &Index, contents: &str) -> usize {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Locator<'a> {
|
impl<'a> Locator<'a> {
|
||||||
pub fn new(contents: &'a str) -> Self {
|
pub const fn new(contents: &'a str) -> Self {
|
||||||
Locator {
|
Self {
|
||||||
contents,
|
contents,
|
||||||
index: OnceCell::new(),
|
index: OnceCell::new(),
|
||||||
}
|
}
|
||||||
|
|
@ -140,11 +140,11 @@ impl<'a> Locator<'a> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn len(&self) -> usize {
|
pub const fn len(&self) -> usize {
|
||||||
self.contents.len()
|
self.contents.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
pub const fn is_empty(&self) -> bool {
|
||||||
self.contents.is_empty()
|
self.contents.is_empty()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -97,7 +97,7 @@ impl From<&Quote> for char {
|
||||||
pub struct Indentation(String);
|
pub struct Indentation(String);
|
||||||
|
|
||||||
impl Indentation {
|
impl Indentation {
|
||||||
pub fn new(indentation: String) -> Self {
|
pub const fn new(indentation: String) -> Self {
|
||||||
Self(indentation)
|
Self(indentation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -142,7 +142,7 @@ impl Default for LineEnding {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LineEnding {
|
impl LineEnding {
|
||||||
pub fn as_str(&self) -> &'static str {
|
pub const fn as_str(&self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
LineEnding::CrLf => "\r\n",
|
LineEnding::CrLf => "\r\n",
|
||||||
LineEnding::Lf => "\n",
|
LineEnding::Lf => "\n",
|
||||||
|
|
|
||||||
|
|
@ -154,7 +154,7 @@ impl fmt::Display for Repr<'_> {
|
||||||
|
|
||||||
/// Returns the outer quotes to use and the number of quotes that need to be
|
/// Returns the outer quotes to use and the number of quotes that need to be
|
||||||
/// escaped.
|
/// escaped.
|
||||||
pub(crate) fn choose_quotes_for_repr(
|
pub(crate) const fn choose_quotes_for_repr(
|
||||||
num_squotes: usize,
|
num_squotes: usize,
|
||||||
num_dquotes: usize,
|
num_dquotes: usize,
|
||||||
quote: Quote,
|
quote: Quote,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue