Skip to content

Commit

Permalink
Fix more panics
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Apr 14, 2023
1 parent e2f4454 commit 1026903
Show file tree
Hide file tree
Showing 19 changed files with 52 additions and 55 deletions.
12 changes: 5 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ path-absolutize = { version = "3.0.14" }
proc-macro2 = { version = "1.0.51" }
quote = { version = "1.0.23" }
regex = { version = "1.7.1" }
ruff_text_size = { git = "https://github.com/charliermarsh/RustPython.git", rev = "834a2c356bbb31c33af900d9d02ce0228a0f6145" }
ruff_text_size = { git = "https://github.com/charliermarsh/RustPython.git", rev = "50b8570bc262098b84dac86092133d3ee26caf23" }
rustc-hash = { version = "1.1.0" }
rustpython-common = { git = "https://github.com/charliermarsh/RustPython.git", rev = "834a2c356bbb31c33af900d9d02ce0228a0f6145" }
rustpython-common = { git = "https://github.com/charliermarsh/RustPython.git", rev = "50b8570bc262098b84dac86092133d3ee26caf23" }
rustpython-parser = { features = [
"lalrpop",
"serde",
], git = "https://github.com/charliermarsh/RustPython.git", rev = "834a2c356bbb31c33af900d9d02ce0228a0f6145" }
], git = "https://github.com/charliermarsh/RustPython.git", rev = "50b8570bc262098b84dac86092133d3ee26caf23" }
schemars = { version = "0.8.12" }
serde = { version = "1.0.152", features = ["derive"] }
serde_json = { version = "1.0.93", features = ["preserve_order"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/jupyter/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub const JUPYTER_NOTEBOOK_EXT: &str = "ipynb";
/// Jupyter Notebook indexing table
///
/// When we lint a jupyter notebook, we have to translate the row/column based on
/// [`crate::message::Location`]
/// [`ruff_text_size::TextSize`]
/// to jupyter notebook cell/row/column.
#[derive(Debug, Eq, PartialEq)]
pub struct JupyterIndex {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub(crate) fn doc_line_too_long(line: &Line, settings: &Settings) -> Option<Diag
let mut start = line.end();
let mut width = line_width;

for c in line.chars() {
for c in line.chars().rev() {
width -= c.width().unwrap_or(0);
start -= c.text_len();
if width == limit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ruff_text_size::{TextLen, TextRange, TextSize};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::newlines::StrExt;
use ruff_python_ast::newlines::UniversalNewlineIterator;
use ruff_python_ast::source_code::Locator;

/// ## What it does
Expand Down Expand Up @@ -73,10 +73,13 @@ pub fn invalid_escape_sequence(
};
let quote_pos = text.find(quote).unwrap();
let prefix = text[..quote_pos].to_lowercase();
let body = &text[(quote_pos + quote.len())..(text.len() - quote.len())];
let body = &text[quote_pos + quote.len()..text.len() - quote.len()];

if !prefix.contains('r') {
for line in body.universal_newlines() {
for line in UniversalNewlineIterator::with_offset(
&body,
start + TextSize::try_from(quote_pos).unwrap() + quote.text_len(),
) {
let mut chars_iter = line.char_indices().peekable();

while let Some((i, c)) = chars_iter.next() {
Expand All @@ -99,11 +102,8 @@ pub fn invalid_escape_sequence(
continue;
}

let location = start
+ line.start()
+ quote.text_len()
+ TextSize::try_from(quote_pos + i).unwrap();
let range = TextRange::at(location, TextSize::from(2));
let location = line.start() + TextSize::try_from(i).unwrap();
let range = TextRange::at(location, next_char.text_len() + TextSize::from(1));
let mut diagnostic = Diagnostic::new(InvalidEscapeSequence(*next_char), range);
if autofix {
diagnostic.set_fix(Edit::insertion(
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/pycodestyle/rules/line_too_long.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) fn line_too_long(line: &Line, settings: &Settings) -> Option<Diagnost
let mut offset = line.end();
let mut width = line_width;

for c in line.chars() {
for c in line.chars().rev() {
width -= c.width().unwrap_or(0);
offset -= c.text_len();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl<'a> LogicalLine<'a> {
}
}

/// Returns the [`Location`] of the first token on the line or [`None`].
/// Returns the [`TextSize`] of the first token on the line or [`None`].
pub fn first_token_location(&self) -> Option<TextSize> {
self.tokens().first().map(|t| t.start())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ E501.py:5:89: E501 Line too long (123 > 88 characters)
8 | """
|

E501.py:16:86: E501 Line too long (95 > 88 characters)
E501.py:16:85: E501 Line too long (95 > 88 characters)
|
16 | _ = "---------------------------------------------------------------------------AAAAAAA"
17 | _ = "---------------------------------------------------------------------------亜亜亜亜亜亜亜"
| ^^^^^ E501
| ^^^^^^^ E501
|

E501.py:25:89: E501 Line too long (127 > 88 characters)
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/pyflakes/rules/unused_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl AlwaysAutofixableViolation for UnusedVariable {
}
}

/// Return the start and end [`Location`] of the token after the next match of
/// Return the [`TextRange`] of the token after the next match of
/// the predicate, skipping over any bracketed expressions.
fn match_token_after<F, T>(located: &Located<T>, locator: &Locator, f: F) -> TextRange
where
Expand Down Expand Up @@ -124,7 +124,7 @@ where
unreachable!("No token after matched");
}

/// Return the start and end [`Location`] of the token matching the predicate,
/// Return the [`TextRange`] of the token matching the predicate,
/// skipping over any bracketed expressions.
fn match_token<F, T>(located: &Located<T>, locator: &Locator, f: F) -> TextRange
where
Expand Down Expand Up @@ -190,7 +190,7 @@ enum DeletionKind {
}

/// Generate a [`Edit`] to remove an unused variable assignment, given the
/// enclosing [`Stmt`] and the [`Range`] of the variable binding.
/// enclosing [`Stmt`] and the [`TextRange`] of the variable binding.
fn remove_unused_variable(
stmt: &Stmt,
range: TextRange,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/pyupgrade/fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ruff_python_ast::source_code::{Locator, Stylist};

use crate::cst::matchers::match_module;

/// Safely adjust the indentation of the indented block at [`Range`].
/// Safely adjust the indentation of the indented block at [`TextRange`].
pub fn adjust_indentation(
range: TextRange,
indentation: &str,
Expand Down
10 changes: 4 additions & 6 deletions crates/ruff_cli/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl Serialize for SerializeMessage<'_> {
noqa_offset: noqa_row,
} = self.message;

let mut s = serializer.serialize_struct("Message", 6)?;
let mut s = serializer.serialize_struct("Message", 5)?;

s.serialize_field("kind", &kind)?;
s.serialize_field("range", &range)?;
Expand Down Expand Up @@ -222,11 +222,9 @@ pub fn get(
let source_files: Vec<_> = sources
.into_iter()
.map(|(filename, text)| {
let mut builder = SourceFileBuilder::from_string(filename);

builder.set_source_text_string(text);

builder.finish()
SourceFileBuilder::from_string(filename)
.source_text_string(text)
.finish()
})
.collect();

Expand Down
1 change: 1 addition & 0 deletions crates/ruff_cli/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ pub fn lint_path(
let imports = imports.unwrap_or_default();

if let Some(err) = parse_error {
// FIXME micha manually print parse errors to get line and column numbers
// Notify the user of any parse errors.
error!(
"{}{}{} {err}",
Expand Down
3 changes: 0 additions & 3 deletions crates/ruff_diagnostics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ rust-version = { workspace = true }
[dependencies]
anyhow = { workspace = true }
log = { workspace = true }
ruff_python_ast = { path = "../ruff_python_ast" }
ruff_text_size = { workspace = true }

rustpython-parser = { workspace = true }
serde = { workspace = true, optional = true, default_features = false, features = [] }
2 changes: 1 addition & 1 deletion crates/ruff_diagnostics/src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Edit {
}
}

/// Creates an edit that inserts `content` at the [`Location`] `at`.
/// Creates an edit that inserts `content` at the [`TextSize`] `at`.
pub fn insertion(content: String, at: TextSize) -> Self {
debug_assert!(!content.is_empty(), "Insert content is empty");

Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_python_ast/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ pub fn has_comments<T>(located: &Located<T>, locator: &Locator) -> bool {
has_comments_in(TextRange::new(start, end), locator)
}

/// Returns `true` if a [`Range`] includes at least one comment.
/// Returns `true` if a [`TextRange`] includes at least one comment.
pub fn has_comments_in(range: TextRange, locator: &Locator) -> bool {
let source = &locator.contents()[range];

Expand Down Expand Up @@ -887,7 +887,7 @@ pub fn trailing_lines_end(stmt: &Stmt, locator: &Locator) -> TextSize {
.map_or(line_end, |l| l.full_end())
}

/// Return the range of the first parenthesis pair after a given [`Location`].
/// Return the range of the first parenthesis pair after a given [`TextSize`].
pub fn match_parens(start: TextSize, locator: &Locator) -> Option<TextRange> {
let contents = &locator.contents()[usize::from(start)..];

Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_python_ast/src/source_code/line_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::num::NonZeroUsize;
use std::ops::Deref;
use std::sync::Arc;

/// Index for fast [`Location`] to [byte offset](TextSize) conversions.
/// Index for fast [byte offset](TextSize) to [`SourceLocation`] conversions.
///
/// Cloning a [`LineIndex`] is cheap because it only requires bumping a reference count.
#[derive(Clone)]
Expand Down Expand Up @@ -105,7 +105,7 @@ impl LineIndex {
} else {
// Don't count the BOM character as a column.
if line_start == TextSize::from(0) && content.starts_with('\u{feff}') {
line_start += '\u{feff}'.text_len();
line_start = '\u{feff}'.text_len();
}

content[TextRange::new(line_start, offset)].chars().count()
Expand Down
11 changes: 5 additions & 6 deletions crates/ruff_python_ast/src/source_code/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,23 +375,22 @@ impl<'a> Locator<'a> {
&self.contents[self.full_lines_range(range)]
}

// TODO remove
/// Take the source code up to the given [`Location`].
/// Take the source code up to the given [`TextSize`].
#[inline]
pub fn up_to(&self, offset: TextSize) -> &'a str {
&self.contents[TextRange::up_to(offset)]
}

/// Take the source code after the given [`Location`].
/// Take the source code after the given [`TextSize`].
#[inline]
pub fn after(&self, offset: TextSize) -> &'a str {
&self.contents[usize::from(offset)..]
}

/// Take the source code between the given [`Range`].
/// Take the source code between the given [`TextRange`].
#[inline]
pub fn slice<R: Into<TextRange>>(&self, range: R) -> &'a str {
&self.contents[range.into()]
pub fn slice(&self, range: TextRange) -> &'a str {
&self.contents[range]
}

/// Return the underlying source code.
Expand Down
17 changes: 10 additions & 7 deletions crates/ruff_python_ast/src/source_code/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn round_trip(code: &str, source_path: &str) -> Result<String, ParseError> {
Ok(generator.generate())
}

/// Gives access to the source code of a file and allows mapping between [`Location`] and byte offsets.
/// Gives access to the source code of a file and allows mapping between [`TextSize`] and [`SourceLocation`].
#[derive(Debug)]
pub struct SourceCode<'src, 'index> {
text: &'src str,
Expand All @@ -45,28 +45,31 @@ impl<'src, 'index> SourceCode<'src, 'index> {
}

/// Computes the one indexed row and column numbers for `offset`.
#[inline]
pub fn source_location(&self, offset: TextSize) -> SourceLocation {
self.index.source_location(offset, self.text)
}

#[inline]
pub fn line_index(&self, offset: TextSize) -> OneIndexed {
self.index.line_index(offset)
}

// TODO inline
/// Take the source code up to the given [`Location`].
/// Take the source code up to the given [`TextSize`].
#[inline]
pub fn up_to(&self, offset: TextSize) -> &'src str {
&self.text[TextRange::up_to(offset)]
}

/// Take the source code after the given [`Location`].
/// Take the source code after the given [`TextSize`].
#[inline]
pub fn after(&self, offset: TextSize) -> &'src str {
&self.text[usize::from(offset)..]
}

/// Take the source code between the given [`Range`].
pub fn slice<R: Into<TextRange>>(&self, range: R) -> &'src str {
&self.text[range.into()]
/// Take the source code between the given [`TextRange`].
pub fn slice(&self, range: TextRange) -> &'src str {
&self.text[range]
}

pub fn line_start(&self, line: OneIndexed) -> TextSize {
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_wasm/tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use js_sys;
use wasm_bindgen_test::*;

use ruff::registry::Rule;
use ruff_python_ast::source_code::{OneIndexed, SourceLocation};
use ruff_wasm::*;

macro_rules! check {
Expand Down

0 comments on commit 1026903

Please sign in to comment.