mirror of https://github.com/astral-sh/ruff
docs(pycodestyle): document rules (#3407)
This commit is contained in:
parent
b983d5eb3f
commit
a3aeec6377
|
|
@ -4,6 +4,24 @@ use ruff_python_ast::types::Range;
|
|||
|
||||
use crate::rules::pycodestyle::helpers::is_ambiguous_name;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for the use of the characters 'l', 'O', or 'I' as class names.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// In some fonts, these characters are indistinguishable from the
|
||||
/// numerals one and zero. When tempted to use 'l', use 'L' instead.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// class I(object):
|
||||
/// ...
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// class Integer(object):
|
||||
/// ...
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct AmbiguousClassName(pub String);
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,24 @@ use ruff_python_ast::types::Range;
|
|||
|
||||
use crate::rules::pycodestyle::helpers::is_ambiguous_name;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for the use of the characters 'l', 'O', or 'I' as function names.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// In some fonts, these characters are indistinguishable from the
|
||||
/// numerals one and zero. When tempted to use 'l', use 'L' instead.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// def l(x):
|
||||
/// ...
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// def long_name(x):
|
||||
/// ...
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct AmbiguousFunctionName(pub String);
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,27 @@ use ruff_python_ast::types::Range;
|
|||
|
||||
use crate::rules::pycodestyle::helpers::is_ambiguous_name;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for the use of the characters 'l', 'O', or 'I' as variable names.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// In some fonts, these characters are indistinguishable from the
|
||||
/// numerals one and zero. When tempted to use 'l', use 'L' instead.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// l = 0
|
||||
/// O = 123
|
||||
/// I = 42
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// L = 0
|
||||
/// o = 123
|
||||
/// i = 42
|
||||
/// ```
|
||||
|
||||
#[violation]
|
||||
pub struct AmbiguousVariableName(pub String);
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,25 @@ use ruff_python_ast::types::Range;
|
|||
use crate::registry::Rule;
|
||||
use crate::settings::{flags, Settings};
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for compound statements (multiple statements on the same line).
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, "compound statements are generally discouraged".
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// if foo == 'blah': do_blah_thing()
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// if foo == 'blah':
|
||||
/// do_blah_thing()
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#other-recommendations)
|
||||
#[violation]
|
||||
pub struct MultipleStatementsOnOneLineColon;
|
||||
|
||||
|
|
@ -19,6 +38,27 @@ impl Violation for MultipleStatementsOnOneLineColon {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for multiline statements on one line.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, including multi-clause statements on the same line is
|
||||
/// discouraged.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// do_one(); do_two(); do_three()
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// do_one()
|
||||
/// do_two()
|
||||
/// do_three()
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#other-recommendations)
|
||||
#[violation]
|
||||
pub struct MultipleStatementsOnOneLineSemicolon;
|
||||
|
||||
|
|
@ -29,6 +69,21 @@ impl Violation for MultipleStatementsOnOneLineSemicolon {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for statements that end with an unnecessary semicolon.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// A trailing semicolon is unnecessary and should be removed.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// do_four(); # useless semicolon
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// do_four()
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct UselessSemicolon;
|
||||
|
||||
|
|
@ -43,6 +98,7 @@ impl AlwaysAutofixableViolation for UselessSemicolon {
|
|||
}
|
||||
}
|
||||
|
||||
/// E701, E702, E703
|
||||
pub fn compound_statements(
|
||||
lxr: &[LexResult],
|
||||
settings: &Settings,
|
||||
|
|
|
|||
|
|
@ -7,6 +7,27 @@ use ruff_python_ast::types::Range;
|
|||
use crate::rules::pycodestyle::helpers::is_overlong;
|
||||
use crate::settings::Settings;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for doc lines that exceed the specified maximum character length.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// For flowing long blocks of text (docstrings or comments), overlong lines
|
||||
/// can hurt readability.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// def function(x):
|
||||
/// """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis auctor purus ut ex fermentum, at maximus est hendrerit."""
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// def function(x):
|
||||
/// """
|
||||
/// Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||
/// Duis auctor purus ut ex fermentum, at maximus est hendrerit.
|
||||
/// """
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct DocLineTooLong(pub usize, pub usize);
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ pub struct IOError {
|
|||
pub message: String,
|
||||
}
|
||||
|
||||
/// E902
|
||||
impl Violation for IOError {
|
||||
#[derive_message_formats]
|
||||
fn message(&self) -> String {
|
||||
|
|
@ -30,6 +31,7 @@ impl Violation for SyntaxError {
|
|||
}
|
||||
}
|
||||
|
||||
/// E901
|
||||
pub fn syntax_error(diagnostics: &mut Vec<Diagnostic>, parse_error: &ParseError) {
|
||||
diagnostics.push(Diagnostic::new(
|
||||
SyntaxError {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,28 @@ use ruff_diagnostics::DiagnosticKind;
|
|||
use ruff_diagnostics::Violation;
|
||||
use ruff_macros::{derive_message_formats, violation};
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for the use of extraneous whitespace after "(".
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// PEP 8 recommends the omission of whitespace in the following cases:
|
||||
/// - "Immediately inside parentheses, brackets or braces."
|
||||
/// - "Immediately before a comma, semicolon, or colon."
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// spam( ham[1], {eggs: 2})
|
||||
/// spam(ham[ 1], {eggs: 2})
|
||||
/// spam(ham[1], { eggs: 2})
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// spam(ham[1], {eggs: 2})
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#pet-peeves)
|
||||
#[violation]
|
||||
pub struct WhitespaceAfterOpenBracket;
|
||||
|
||||
|
|
@ -17,6 +39,28 @@ impl Violation for WhitespaceAfterOpenBracket {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for the use of extraneous whitespace before ")".
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// PEP 8 recommends the omission of whitespace in the following cases:
|
||||
/// - "Immediately inside parentheses, brackets or braces."
|
||||
/// - "Immediately before a comma, semicolon, or colon."
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// spam(ham[1], {eggs: 2} )
|
||||
/// spam(ham[1 ], {eggs: 2})
|
||||
/// spam(ham[1], {eggs: 2 })
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// spam(ham[1], {eggs: 2})
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#pet-peeves)
|
||||
#[violation]
|
||||
pub struct WhitespaceBeforeCloseBracket;
|
||||
|
||||
|
|
@ -27,6 +71,26 @@ impl Violation for WhitespaceBeforeCloseBracket {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for the use of extraneous whitespace before ",", ";" or ":".
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// PEP 8 recommends the omission of whitespace in the following cases:
|
||||
/// - "Immediately inside parentheses, brackets or braces."
|
||||
/// - "Immediately before a comma, semicolon, or colon."
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// if x == 4: print(x, y); x, y = y , x
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// if x == 4: print(x, y); x, y = y, x
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#pet-peeves)
|
||||
#[violation]
|
||||
pub struct WhitespaceBeforePunctuation;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,25 @@ use ruff_python_ast::types::Range;
|
|||
|
||||
use crate::checkers::ast::Checker;
|
||||
|
||||
/// ## What it does
|
||||
/// Check for multiple imports on one line.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, "imports should usually be on separate lines."
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// import sys, os
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// import os
|
||||
/// import sys
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#imports)
|
||||
#[violation]
|
||||
pub struct MultipleImportsOnOneLine;
|
||||
|
||||
|
|
@ -16,6 +35,33 @@ impl Violation for MultipleImportsOnOneLine {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for imports that are not at the top of the file.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, "imports are always put at the top of the file, just after any
|
||||
/// module comments and docstrings, and before module globals and constants."
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// 'One string'
|
||||
/// "Two string"
|
||||
/// a = 1
|
||||
/// import os
|
||||
/// from sys import x
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// import os
|
||||
/// from sys import x
|
||||
/// 'One string'
|
||||
/// "Two string"
|
||||
/// a = 1
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#imports)
|
||||
#[violation]
|
||||
pub struct ModuleImportNotAtTopOfFile;
|
||||
|
||||
|
|
@ -26,6 +72,7 @@ impl Violation for ModuleImportNotAtTopOfFile {
|
|||
}
|
||||
}
|
||||
|
||||
/// E401
|
||||
pub fn multiple_imports_on_one_line(checker: &mut Checker, stmt: &Stmt, names: &[Alias]) {
|
||||
if names.len() > 1 {
|
||||
checker
|
||||
|
|
@ -34,6 +81,7 @@ pub fn multiple_imports_on_one_line(checker: &mut Checker, stmt: &Stmt, names: &
|
|||
}
|
||||
}
|
||||
|
||||
/// E402
|
||||
pub fn module_import_not_at_top_of_file(checker: &mut Checker, stmt: &Stmt) {
|
||||
if checker.ctx.seen_import_boundary && stmt.location.column() == 0 {
|
||||
checker.diagnostics.push(Diagnostic::new(
|
||||
|
|
|
|||
|
|
@ -6,6 +6,26 @@ use ruff_macros::{derive_message_formats, violation};
|
|||
|
||||
use crate::rules::pycodestyle::logical_lines::LogicalLine;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for indentation with a non-multiple of 4 spaces.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, 4 spaces per indentation level should be preferred.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// if True:
|
||||
/// a = 1
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// if True:
|
||||
/// a = 1
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#indentation)
|
||||
#[violation]
|
||||
pub struct IndentationWithInvalidMultiple {
|
||||
pub indent_size: usize,
|
||||
|
|
@ -19,6 +39,26 @@ impl Violation for IndentationWithInvalidMultiple {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for indentation of comments with a non-multiple of 4 spaces.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, 4 spaces per indentation level should be preferred.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// if True:
|
||||
/// # a = 1
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// if True:
|
||||
/// # a = 1
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#indentation)
|
||||
#[violation]
|
||||
pub struct IndentationWithInvalidMultipleComment {
|
||||
pub indent_size: usize,
|
||||
|
|
@ -32,6 +72,28 @@ impl Violation for IndentationWithInvalidMultipleComment {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for indented blocks that are lacking indentation.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// All indented blocks should be indented; otherwise, they are not valid
|
||||
/// Python syntax.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// for item in items:
|
||||
/// pass
|
||||
///
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// for item in items:
|
||||
/// pass
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#indentation)
|
||||
#[violation]
|
||||
pub struct NoIndentedBlock;
|
||||
|
||||
|
|
@ -42,6 +104,29 @@ impl Violation for NoIndentedBlock {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for comments in a code blocks that are lacking indentation.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Comments within an indented block should themselves be indented, to
|
||||
/// indicate that they are part of the block.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// for item in items:
|
||||
/// # Hi
|
||||
/// pass
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// for item in items:
|
||||
/// # Hi
|
||||
/// pass
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#indentation)
|
||||
#[violation]
|
||||
pub struct NoIndentedBlockComment;
|
||||
|
||||
|
|
@ -52,6 +137,26 @@ impl Violation for NoIndentedBlockComment {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for unexpected indentation.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Indentation outside of a code block is not valid Python syntax.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// a = 1
|
||||
/// b = 2
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// a = 1
|
||||
/// b = 2
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#indentation)
|
||||
#[violation]
|
||||
pub struct UnexpectedIndentation;
|
||||
|
||||
|
|
@ -62,6 +167,26 @@ impl Violation for UnexpectedIndentation {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for unexpected indentation of comment.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Comments should match the indentation of the containing code block.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// a = 1
|
||||
/// # b = 2
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// a = 1
|
||||
/// # b = 2
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#indentation)
|
||||
#[violation]
|
||||
pub struct UnexpectedIndentationComment;
|
||||
|
||||
|
|
@ -72,6 +197,28 @@ impl Violation for UnexpectedIndentationComment {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for over-indented code.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, 4 spaces per indentation level should be preferred. Increased
|
||||
/// indentation can lead to inconsistent formatting, which can hurt
|
||||
/// readability.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// for item in items:
|
||||
/// pass
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// for item in items:
|
||||
/// pass
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#indentation)
|
||||
#[violation]
|
||||
pub struct OverIndented;
|
||||
|
||||
|
|
@ -82,7 +229,7 @@ impl Violation for OverIndented {
|
|||
}
|
||||
}
|
||||
|
||||
/// E111
|
||||
/// E111, E114, E112, E113, E115, E116, E117
|
||||
#[cfg(feature = "logical_lines")]
|
||||
pub fn indentation(
|
||||
logical_line: &LogicalLine,
|
||||
|
|
|
|||
|
|
@ -7,6 +7,21 @@ use ruff_macros::{derive_message_formats, violation};
|
|||
use ruff_python_ast::source_code::Locator;
|
||||
use ruff_python_ast::types::Range;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for invalid escape sequences.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Invalid escape sequences are deprecated in Python 3.6.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// regex = '\.png$'
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// regex = r'\.png$'
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct InvalidEscapeSequence(pub char);
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,30 @@ use ruff_python_ast::whitespace::leading_space;
|
|||
use crate::checkers::ast::Checker;
|
||||
use crate::registry::AsRule;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for lambda expressions which are assigned to a variable.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, you should "Always use a def statement instead of an assignment
|
||||
/// statement that binds a lambda expression directly to an identifier."
|
||||
///
|
||||
/// Using a `def` statement leads to better tracebacks, and the assignment
|
||||
/// itself negates the primary benefit of using a `lambda` expression (i.e.,
|
||||
/// that it can be embedded inside another expression).
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// f = lambda x: 2*x
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// def f(x):
|
||||
/// return 2 * x
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#programming-recommendations)
|
||||
#[violation]
|
||||
pub struct LambdaAssignment {
|
||||
pub name: String,
|
||||
|
|
|
|||
|
|
@ -7,6 +7,24 @@ use ruff_python_ast::types::Range;
|
|||
use crate::rules::pycodestyle::helpers::is_overlong;
|
||||
use crate::settings::Settings;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for lines that exceed the specified maximum character length.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Overlong lines can hurt readability.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// my_function(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10)
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// my_function(
|
||||
/// param1, param2, param3, param4, param5,
|
||||
/// param6, param7, param8, param9, param10
|
||||
/// )
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct LineTooLong(pub usize, pub usize);
|
||||
|
||||
|
|
|
|||
|
|
@ -28,6 +28,26 @@ impl From<&Cmpop> for EqCmpop {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for comparisons to `None` which are not using the `is` operator.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, "Comparisons to singletons like None should always be done with
|
||||
/// is or is not, never the equality operators."
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// if arg != None:
|
||||
/// if None == arg:
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// if arg is not None:
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#programming-recommendations)
|
||||
#[violation]
|
||||
pub struct NoneComparison(pub EqCmpop);
|
||||
|
||||
|
|
@ -50,6 +70,27 @@ impl AlwaysAutofixableViolation for NoneComparison {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for comparisons to booleans which are not using the `is` operator.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, "Comparisons to singletons like None should always be done with
|
||||
/// is or is not, never the equality operators."
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// if arg == True:
|
||||
/// if False == arg:
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// if arg is True:
|
||||
/// if arg is False:
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#programming-recommendations)
|
||||
#[violation]
|
||||
pub struct TrueFalseComparison(pub bool, pub EqCmpop);
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,26 @@ use ruff_macros::{derive_message_formats, violation};
|
|||
use ruff_python_ast::types::Range;
|
||||
use ruff_python_ast::whitespace::leading_space;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for mixed tabs and spaces in indentation.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Never mix tabs and spaces.
|
||||
///
|
||||
/// The most popular way of indenting Python is with spaces only. The
|
||||
/// second-most popular way is with tabs only. Code indented with a
|
||||
/// mixture of tabs and spaces should be converted to using spaces
|
||||
/// exclusively.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// if a == 0:\n a = 1\n\tb = 1
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// if a == 0:\n a = 1\n b = 1
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct MixedSpacesAndTabs;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,22 @@ use ruff_macros::{derive_message_formats, violation};
|
|||
use ruff_python_ast::source_code::Stylist;
|
||||
use ruff_python_ast::types::Range;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for files missing a new line at the end of the file.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Trailing blank lines are superfluous.
|
||||
/// However the last line should end with a new line.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// spam(1)
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// spam(1)\n
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct NoNewLineAtEndOfFile;
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,25 @@ use crate::checkers::ast::Checker;
|
|||
use crate::registry::AsRule;
|
||||
use crate::rules::pycodestyle::helpers::compare;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for negative comparison using `not {foo} in {bar}`.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Negative comparison should be done using `not in`.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// Z = not X in Y
|
||||
/// if not X.B in Y:\n pass
|
||||
///
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// if x not in y:\n pass
|
||||
/// assert (X in Y or X is Z)
|
||||
///
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct NotInTest;
|
||||
|
||||
|
|
@ -22,6 +41,25 @@ impl AlwaysAutofixableViolation for NotInTest {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for negative comparison using `not {foo} is {bar}`.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Negative comparison should be done using `is not`.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// if not X is Y:
|
||||
/// pass
|
||||
/// Z = not X.B is Y
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// if not (X in Y):
|
||||
/// pass
|
||||
/// zz = x is not y
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct NotIsTest;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,25 @@ use ruff_diagnostics::DiagnosticKind;
|
|||
use ruff_diagnostics::Violation;
|
||||
use ruff_macros::{derive_message_formats, violation};
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for extraneous tabs before an operator.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, operators should be surrounded by at most a single space on either
|
||||
/// side.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// a = 4\t+ 5
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// a = 12 + 3
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements)
|
||||
#[violation]
|
||||
pub struct TabBeforeOperator;
|
||||
|
||||
|
|
@ -17,6 +36,25 @@ impl Violation for TabBeforeOperator {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for extraneous whitespace before an operator.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, operators should be surrounded by at most a single space on either
|
||||
/// side.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// a = 4 + 5
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// a = 12 + 3
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements)
|
||||
#[violation]
|
||||
pub struct MultipleSpacesBeforeOperator;
|
||||
|
||||
|
|
@ -27,6 +65,25 @@ impl Violation for MultipleSpacesBeforeOperator {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for extraneous tabs after an operator.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, operators should be surrounded by at most a single space on either
|
||||
/// side.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// a = 4 +\t5
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// a = 12 + 3
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements)
|
||||
#[violation]
|
||||
pub struct TabAfterOperator;
|
||||
|
||||
|
|
@ -37,6 +94,25 @@ impl Violation for TabAfterOperator {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for extraneous whitespace after an operator.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, operators should be surrounded by at most a single space on either
|
||||
/// side.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// a = 4 + 5
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// a = 12 + 3
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements)
|
||||
#[violation]
|
||||
pub struct MultipleSpacesAfterOperator;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,25 @@ use ruff_python_ast::types::Range;
|
|||
use crate::registry::Rule;
|
||||
use crate::settings::{flags, Settings};
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for superfluous trailing whitespace.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, "avoid trailing whitespace anywhere. Because it’s usually
|
||||
/// invisible, it can be confusing"
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// spam(1) \n#
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// spam(1)\n#
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#other-recommendations)
|
||||
#[violation]
|
||||
pub struct TrailingWhitespace;
|
||||
|
||||
|
|
@ -21,6 +40,26 @@ impl AlwaysAutofixableViolation for TrailingWhitespace {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for superfluous whitespace in blank lines.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP 8, "avoid trailing whitespace anywhere. Because it’s usually
|
||||
/// invisible, it can be confusing"
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// class Foo(object):\n \n bang = 12
|
||||
///
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// class Foo(object):\n\n bang = 12
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#other-recommendations)
|
||||
#[violation]
|
||||
pub struct BlankLineContainsWhitespace;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,24 @@ use ruff_diagnostics::{Diagnostic, Violation};
|
|||
use ruff_macros::{derive_message_formats, violation};
|
||||
use ruff_python_ast::types::Range;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for object type comparisons without using isinstance().
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Do not compare types directly.
|
||||
/// When checking if an object is a instance of a certain type, keep in mind that it might
|
||||
/// be subclassed. E.g. `bool` inherits from `int` or `Exception` inherits from `BaseException`.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// if type(obj) is type(1):
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// if isinstance(obj, int):
|
||||
/// if type(a1) is type(b1):
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct TypeComparison;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,21 @@ use ruff_diagnostics::DiagnosticKind;
|
|||
use ruff_diagnostics::Violation;
|
||||
use ruff_macros::{derive_message_formats, violation};
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for extraneous whitespace after keywords.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
///
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// True and False
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// True and False
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct MultipleSpacesAfterKeyword;
|
||||
|
||||
|
|
@ -17,6 +32,22 @@ impl Violation for MultipleSpacesAfterKeyword {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for extraneous whitespace before keywords.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
///
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// True and False
|
||||
///
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// True and False
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct MultipleSpacesBeforeKeyword;
|
||||
|
||||
|
|
@ -27,6 +58,22 @@ impl Violation for MultipleSpacesBeforeKeyword {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for extraneous tabs after keywords.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
///
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// True and\tFalse
|
||||
///
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// True and False
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct TabAfterKeyword;
|
||||
|
||||
|
|
@ -37,6 +84,22 @@ impl Violation for TabAfterKeyword {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for extraneous tabs before keywords.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
///
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// True\tand False
|
||||
///
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// True and False
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct TabBeforeKeyword;
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,25 @@ use ruff_macros::{derive_message_formats, violation};
|
|||
use ruff_python_ast::source_code::Locator;
|
||||
use ruff_python_ast::types::Range;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks if inline comments are separated by at least two spaces.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// An inline comment is a comment on the same line as a statement.
|
||||
///
|
||||
/// Per PEP8, inline comments should be separated by at least two spaces from
|
||||
/// the preceding statement.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// x = x + 1 # Increment x
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// x = x + 1 # Increment x
|
||||
/// x = x + 1 # Increment x
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct TooFewSpacesBeforeInlineComment;
|
||||
|
||||
|
|
@ -19,6 +38,29 @@ impl Violation for TooFewSpacesBeforeInlineComment {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks if one space is used after inline comments.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// An inline comment is a comment on the same line as a statement.
|
||||
///
|
||||
/// Per PEP8, inline comments should start with a # and a single space.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// x = x + 1 #Increment x
|
||||
/// x = x + 1 # Increment x
|
||||
/// x = x + 1 # \xa0Increment x
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// x = x + 1 # Increment x
|
||||
/// x = x + 1 # Increment x
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#comments)
|
||||
#[violation]
|
||||
pub struct NoSpaceAfterInlineComment;
|
||||
|
||||
|
|
@ -29,6 +71,29 @@ impl Violation for NoSpaceAfterInlineComment {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks if one space is used after block comments.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP8, "Block comments generally consist of one or more paragraphs built
|
||||
/// out of complete sentences, with each sentence ending in a period."
|
||||
///
|
||||
/// Block comments should start with a # and a single space.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// #Block comment
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// # Block comments:
|
||||
/// # - Block comment list
|
||||
/// # \xa0- Block comment list
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#comments)
|
||||
#[violation]
|
||||
pub struct NoSpaceAfterBlockComment;
|
||||
|
||||
|
|
@ -39,6 +104,30 @@ impl Violation for NoSpaceAfterBlockComment {
|
|||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
/// Checks if block comments start with a single "#".
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Per PEP8, "Block comments generally consist of one or more paragraphs built
|
||||
/// out of complete sentences, with each sentence ending in a period."
|
||||
///
|
||||
/// Each line of a block comment should start with a # and a single space.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// ### Block comment
|
||||
///
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// # Block comments:
|
||||
/// # - Block comment list
|
||||
/// # \xa0- Block comment list
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://peps.python.org/pep-0008/#comments)
|
||||
#[violation]
|
||||
pub struct MultipleLeadingHashesForBlockComment;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue