mirror of https://github.com/astral-sh/ruff
perf: Avoid `Vec<&str>` allocation during line length checking (#95)
This commit is contained in:
parent
4319bd1755
commit
507fecfd9a
|
|
@ -3,6 +3,22 @@ use rustpython_parser::ast::Location;
|
||||||
use crate::checks::{Check, CheckKind};
|
use crate::checks::{Check, CheckKind};
|
||||||
use crate::settings::Settings;
|
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 {
|
||||||
|
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
|
||||||
|
!(first == "#" && chunks.next().is_none())
|
||||||
|
} else {
|
||||||
|
// Single word / no printable chars - no way to make the line shorter
|
||||||
|
false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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_ling = settings.select.contains(CheckKind::LineTooLong.code());
|
let enforce_line_too_ling = settings.select.contains(CheckKind::LineTooLong.code());
|
||||||
|
|
||||||
|
|
@ -18,16 +34,13 @@ pub fn check_lines(checks: &mut Vec<Check>, contents: &str, settings: &Settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enforce line length.
|
// Enforce line length.
|
||||||
if enforce_line_too_ling && line.len() > settings.line_length {
|
if enforce_line_too_ling && should_enforce_line_length(line, settings.line_length) {
|
||||||
let chunks: Vec<&str> = line.split_whitespace().collect();
|
let check = Check {
|
||||||
if !(chunks.len() == 1 || (chunks.len() == 2 && chunks[0] == "#")) {
|
kind: CheckKind::LineTooLong,
|
||||||
let check = Check {
|
location: Location::new(row + 1, settings.line_length + 1),
|
||||||
kind: CheckKind::LineTooLong,
|
};
|
||||||
location: Location::new(row + 1, settings.line_length + 1),
|
if !check.is_inline_ignored(line) {
|
||||||
};
|
line_checks.push(check);
|
||||||
if !check.is_inline_ignored(line) {
|
|
||||||
line_checks.push(check);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue