diff --git a/examples/generate_rules_table.rs b/examples/generate_rules_table.rs index 410b2ab027..4eed9e794a 100644 --- a/examples/generate_rules_table.rs +++ b/examples/generate_rules_table.rs @@ -16,7 +16,7 @@ fn main() { CheckKind::IfTuple, CheckKind::ImportStarUsage, CheckKind::LateFutureImport, - CheckKind::LineTooLong, + CheckKind::LineTooLong(89, 88), CheckKind::ModuleImportNotAtTopOfFile, CheckKind::MultiValueRepeatedKeyLiteral, CheckKind::MultiValueRepeatedKeyVariable("...".to_string()), diff --git a/src/check_lines.rs b/src/check_lines.rs index 9fd3dbbb54..fc11e9baa0 100644 --- a/src/check_lines.rs +++ b/src/check_lines.rs @@ -1,11 +1,11 @@ use rustpython_parser::ast::Location; -use crate::checks::{Check, CheckKind}; +use crate::checks::{Check, CheckCode, CheckKind}; use crate::settings::Settings; /// Whether the given line is too long and should be reported. -fn should_enforce_line_length(line: &str, limit: usize) -> bool { - if line.len() > limit { +fn should_enforce_line_length(line: &str, length: usize, limit: usize) -> bool { + if length > limit { let mut chunks = line.split_whitespace(); if let (Some(first), Some(_)) = (chunks.next(), chunks.next()) { // 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, 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 ignored = vec![]; @@ -34,13 +34,16 @@ pub fn check_lines(checks: &mut Vec, contents: &str, settings: &Settings) } // Enforce line length. - if enforce_line_too_long && should_enforce_line_length(line, settings.line_length) { - let check = Check::new( - CheckKind::LineTooLong, - Location::new(row + 1, settings.line_length + 1), - ); - if !check.is_inline_ignored(line) { - line_checks.push(check); + 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( + CheckKind::LineTooLong(line_length, settings.line_length), + Location::new(row + 1, settings.line_length + 1), + ); + if !check.is_inline_ignored(line) { + line_checks.push(check); + } } } } diff --git a/src/checks.rs b/src/checks.rs index c3973fd460..564c2aecba 100644 --- a/src/checks.rs +++ b/src/checks.rs @@ -193,7 +193,7 @@ pub enum CheckKind { IfTuple, ImportStarUsage, LateFutureImport, - LineTooLong, + LineTooLong(usize, usize), ModuleImportNotAtTopOfFile, MultiValueRepeatedKeyLiteral, MultiValueRepeatedKeyVariable(String), @@ -231,7 +231,7 @@ impl CheckKind { CheckKind::IfTuple => "IfTuple", CheckKind::ImportStarUsage => "ImportStarUsage", CheckKind::LateFutureImport => "LateFutureImport", - CheckKind::LineTooLong => "LineTooLong", + CheckKind::LineTooLong(_, _) => "LineTooLong", CheckKind::DoNotAssignLambda => "DoNotAssignLambda", CheckKind::ModuleImportNotAtTopOfFile => "ModuleImportNotAtTopOfFile", CheckKind::MultiValueRepeatedKeyLiteral => "MultiValueRepeatedKeyLiteral", @@ -269,7 +269,7 @@ impl CheckKind { CheckKind::IfTuple => &CheckCode::F634, CheckKind::ImportStarUsage => &CheckCode::F403, CheckKind::LateFutureImport => &CheckCode::F404, - CheckKind::LineTooLong => &CheckCode::E501, + CheckKind::LineTooLong(_, _) => &CheckCode::E501, CheckKind::DoNotAssignLambda => &CheckCode::E731, CheckKind::AmbiguousVariableName(_) => &CheckCode::E741, CheckKind::AmbiguousClassName(_) => &CheckCode::E742, @@ -322,7 +322,9 @@ impl CheckKind { CheckKind::LateFutureImport => { "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 => { "Do not assign a lambda expression, use a def".to_string() } @@ -421,7 +423,7 @@ impl CheckKind { CheckKind::IfTuple => false, CheckKind::ImportStarUsage => false, CheckKind::LateFutureImport => false, - CheckKind::LineTooLong => false, + CheckKind::LineTooLong(_, _) => false, CheckKind::ModuleImportNotAtTopOfFile => false, CheckKind::MultiValueRepeatedKeyLiteral => false, CheckKind::MultiValueRepeatedKeyVariable(_) => false, diff --git a/src/linter.rs b/src/linter.rs index e16c32190e..15296ea24f 100644 --- a/src/linter.rs +++ b/src/linter.rs @@ -128,7 +128,7 @@ mod tests { )?; actual.sort_by_key(|check| check.location); let expected = vec![Check { - kind: CheckKind::LineTooLong, + kind: CheckKind::LineTooLong(123, 88), location: Location::new(5, 89), fix: None, }];