diff --git a/src/linter.rs b/src/linter.rs index 04f45d2c55..5c8be76bd1 100644 --- a/src/linter.rs +++ b/src/linter.rs @@ -141,13 +141,7 @@ pub fn lint_stdin( // Convert to messages. Ok(checks .into_iter() - .map(|check| Message { - kind: check.kind, - fixed: check.fix.map(|fix| fix.applied).unwrap_or_default(), - location: check.location, - end_location: check.end_location, - filename: path.to_string_lossy().to_string(), - }) + .map(|check| Message::from_check(path.to_string_lossy().to_string(), check)) .collect()) } @@ -189,13 +183,7 @@ pub fn lint_path( // Convert to messages. let messages: Vec = checks .into_iter() - .map(|check| Message { - kind: check.kind, - fixed: check.fix.map(|fix| fix.applied).unwrap_or_default(), - location: check.location, - end_location: check.end_location, - filename: path.to_string_lossy().to_string(), - }) + .map(|check| Message::from_check(path.to_string_lossy().to_string(), check)) .collect(); #[cfg(not(target_family = "wasm"))] cache::set(path, &metadata, settings, autofix, &messages, mode); diff --git a/src/message.rs b/src/message.rs index 774f4e7e62..1463d94153 100644 --- a/src/message.rs +++ b/src/message.rs @@ -6,7 +6,7 @@ use colored::Colorize; use rustpython_parser::ast::Location; use serde::{Deserialize, Serialize}; -use crate::checks::CheckKind; +use crate::checks::{Check, CheckKind}; use crate::fs::relativize_path; #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -18,6 +18,18 @@ pub struct Message { pub filename: String, } +impl Message { + pub fn from_check(filename: String, check: Check) -> Self { + Self { + kind: check.kind, + fixed: check.fix.map(|fix| fix.applied).unwrap_or_default(), + location: Location::new(check.location.row(), check.location.column() + 1), + end_location: Location::new(check.end_location.row(), check.end_location.column() + 1), + filename, + } + } +} + impl Ord for Message { fn cmp(&self, other: &Self) -> Ordering { (&self.filename, self.location.row(), self.location.column()).cmp(&( diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 525f04bda6..c2abaa09a9 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -18,7 +18,7 @@ fn test_stdin_error() -> Result<()> { .write_stdin("import os\n") .assert() .failure(); - assert!(str::from_utf8(&output.get_output().stdout)?.contains("-:1:0: F401")); + assert!(str::from_utf8(&output.get_output().stdout)?.contains("-:1:1: F401")); Ok(()) } @@ -30,7 +30,7 @@ fn test_stdin_filename() -> Result<()> { .write_stdin("import os\n") .assert() .failure(); - assert!(str::from_utf8(&output.get_output().stdout)?.contains("F401.py:1:0: F401")); + assert!(str::from_utf8(&output.get_output().stdout)?.contains("F401.py:1:1: F401")); Ok(()) }