Include line length in E501 messages

This commit is contained in:
Charlie Marsh 2022-09-11 22:53:58 -04:00
parent 546be5692a
commit ff631d39c0
4 changed files with 23 additions and 18 deletions

View File

@ -16,7 +16,7 @@ fn main() {
CheckKind::IfTuple, CheckKind::IfTuple,
CheckKind::ImportStarUsage, CheckKind::ImportStarUsage,
CheckKind::LateFutureImport, CheckKind::LateFutureImport,
CheckKind::LineTooLong, CheckKind::LineTooLong(89, 88),
CheckKind::ModuleImportNotAtTopOfFile, CheckKind::ModuleImportNotAtTopOfFile,
CheckKind::MultiValueRepeatedKeyLiteral, CheckKind::MultiValueRepeatedKeyLiteral,
CheckKind::MultiValueRepeatedKeyVariable("...".to_string()), CheckKind::MultiValueRepeatedKeyVariable("...".to_string()),

View File

@ -1,11 +1,11 @@
use rustpython_parser::ast::Location; use rustpython_parser::ast::Location;
use crate::checks::{Check, CheckKind}; use crate::checks::{Check, CheckCode, CheckKind};
use crate::settings::Settings; use crate::settings::Settings;
/// Whether the given line is too long and should be reported. /// Whether the given line is too long and should be reported.
fn should_enforce_line_length(line: &str, limit: usize) -> bool { fn should_enforce_line_length(line: &str, length: usize, limit: usize) -> bool {
if line.len() > limit { if length > limit {
let mut chunks = line.split_whitespace(); let mut chunks = line.split_whitespace();
if let (Some(first), Some(_)) = (chunks.next(), chunks.next()) { if let (Some(first), Some(_)) = (chunks.next(), chunks.next()) {
// Do not enforce the line length for commented lines with a single word // Do not enforce the line length for commented lines with a single word
@ -20,7 +20,7 @@ fn should_enforce_line_length(line: &str, limit: usize) -> bool {
} }
pub fn check_lines(checks: &mut Vec<Check>, contents: &str, settings: &Settings) { pub fn check_lines(checks: &mut Vec<Check>, contents: &str, settings: &Settings) {
let enforce_line_too_long = settings.select.contains(CheckKind::LineTooLong.code()); let enforce_line_too_long = settings.select.contains(&CheckCode::E501);
let mut line_checks = vec![]; let mut line_checks = vec![];
let mut ignored = vec![]; let mut ignored = vec![];
@ -34,9 +34,11 @@ pub fn check_lines(checks: &mut Vec<Check>, contents: &str, settings: &Settings)
} }
// Enforce line length. // Enforce line length.
if enforce_line_too_long && should_enforce_line_length(line, settings.line_length) { if enforce_line_too_long {
let line_length = line.len();
if should_enforce_line_length(line, line_length, settings.line_length) {
let check = Check::new( let check = Check::new(
CheckKind::LineTooLong, CheckKind::LineTooLong(line_length, settings.line_length),
Location::new(row + 1, settings.line_length + 1), Location::new(row + 1, settings.line_length + 1),
); );
if !check.is_inline_ignored(line) { if !check.is_inline_ignored(line) {
@ -44,6 +46,7 @@ pub fn check_lines(checks: &mut Vec<Check>, contents: &str, settings: &Settings)
} }
} }
} }
}
ignored.sort(); ignored.sort();
for index in ignored.iter().rev() { for index in ignored.iter().rev() {
checks.swap_remove(*index); checks.swap_remove(*index);

View File

@ -193,7 +193,7 @@ pub enum CheckKind {
IfTuple, IfTuple,
ImportStarUsage, ImportStarUsage,
LateFutureImport, LateFutureImport,
LineTooLong, LineTooLong(usize, usize),
ModuleImportNotAtTopOfFile, ModuleImportNotAtTopOfFile,
MultiValueRepeatedKeyLiteral, MultiValueRepeatedKeyLiteral,
MultiValueRepeatedKeyVariable(String), MultiValueRepeatedKeyVariable(String),
@ -231,7 +231,7 @@ impl CheckKind {
CheckKind::IfTuple => "IfTuple", CheckKind::IfTuple => "IfTuple",
CheckKind::ImportStarUsage => "ImportStarUsage", CheckKind::ImportStarUsage => "ImportStarUsage",
CheckKind::LateFutureImport => "LateFutureImport", CheckKind::LateFutureImport => "LateFutureImport",
CheckKind::LineTooLong => "LineTooLong", CheckKind::LineTooLong(_, _) => "LineTooLong",
CheckKind::DoNotAssignLambda => "DoNotAssignLambda", CheckKind::DoNotAssignLambda => "DoNotAssignLambda",
CheckKind::ModuleImportNotAtTopOfFile => "ModuleImportNotAtTopOfFile", CheckKind::ModuleImportNotAtTopOfFile => "ModuleImportNotAtTopOfFile",
CheckKind::MultiValueRepeatedKeyLiteral => "MultiValueRepeatedKeyLiteral", CheckKind::MultiValueRepeatedKeyLiteral => "MultiValueRepeatedKeyLiteral",
@ -269,7 +269,7 @@ impl CheckKind {
CheckKind::IfTuple => &CheckCode::F634, CheckKind::IfTuple => &CheckCode::F634,
CheckKind::ImportStarUsage => &CheckCode::F403, CheckKind::ImportStarUsage => &CheckCode::F403,
CheckKind::LateFutureImport => &CheckCode::F404, CheckKind::LateFutureImport => &CheckCode::F404,
CheckKind::LineTooLong => &CheckCode::E501, CheckKind::LineTooLong(_, _) => &CheckCode::E501,
CheckKind::DoNotAssignLambda => &CheckCode::E731, CheckKind::DoNotAssignLambda => &CheckCode::E731,
CheckKind::AmbiguousVariableName(_) => &CheckCode::E741, CheckKind::AmbiguousVariableName(_) => &CheckCode::E741,
CheckKind::AmbiguousClassName(_) => &CheckCode::E742, CheckKind::AmbiguousClassName(_) => &CheckCode::E742,
@ -322,7 +322,9 @@ impl CheckKind {
CheckKind::LateFutureImport => { CheckKind::LateFutureImport => {
"from __future__ imports must occur at the beginning of the file".to_string() "from __future__ imports must occur at the beginning of the file".to_string()
} }
CheckKind::LineTooLong => "Line too long".to_string(), CheckKind::LineTooLong(length, limit) => {
format!("Line too long ({length} > {limit} characters)")
}
CheckKind::DoNotAssignLambda => { CheckKind::DoNotAssignLambda => {
"Do not assign a lambda expression, use a def".to_string() "Do not assign a lambda expression, use a def".to_string()
} }
@ -421,7 +423,7 @@ impl CheckKind {
CheckKind::IfTuple => false, CheckKind::IfTuple => false,
CheckKind::ImportStarUsage => false, CheckKind::ImportStarUsage => false,
CheckKind::LateFutureImport => false, CheckKind::LateFutureImport => false,
CheckKind::LineTooLong => false, CheckKind::LineTooLong(_, _) => false,
CheckKind::ModuleImportNotAtTopOfFile => false, CheckKind::ModuleImportNotAtTopOfFile => false,
CheckKind::MultiValueRepeatedKeyLiteral => false, CheckKind::MultiValueRepeatedKeyLiteral => false,
CheckKind::MultiValueRepeatedKeyVariable(_) => false, CheckKind::MultiValueRepeatedKeyVariable(_) => false,

View File

@ -128,7 +128,7 @@ mod tests {
)?; )?;
actual.sort_by_key(|check| check.location); actual.sort_by_key(|check| check.location);
let expected = vec![Check { let expected = vec![Check {
kind: CheckKind::LineTooLong, kind: CheckKind::LineTooLong(123, 88),
location: Location::new(5, 89), location: Location::new(5, 89),
fix: None, fix: None,
}]; }];