Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resolver = "2"

[workspace.package]
edition = "2021"
rust-version = "1.84"
rust-version = "1.85"
homepage = "https://docs.astral.sh/ruff"
documentation = "https://docs.astral.sh/ruff"
repository = "https://github.com/astral-sh/ruff"
Expand Down Expand Up @@ -215,6 +215,7 @@ similar_names = "allow"
single_match_else = "allow"
too_many_lines = "allow"
needless_continue = "allow" # An explicit continue can be more readable, especially if the alternative is an empty block.
unnecessary_debug_formatting = "allow" # too many instances, the display also doesn't quote the path which is often desired in logs where we use them the most often.
# Without the hashes we run into a `rustfmt` bug in some snapshot tests, see #13250
needless_raw_string_hashes = "allow"
# Disallowed restriction lints
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_db/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ struct SourceTextInner {
#[derive(Eq, PartialEq)]
enum SourceTextKind {
Text(String),
Notebook(Notebook),
Notebook(Box<Notebook>),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notebooks are massive and clippy complaint that the enum variants where significantly different in size (24 vs > 500)

}

impl From<String> for SourceTextKind {
Expand All @@ -144,7 +144,7 @@ impl From<String> for SourceTextKind {

impl From<Notebook> for SourceTextKind {
fn from(notebook: Notebook) -> Self {
SourceTextKind::Notebook(notebook)
SourceTextKind::Notebook(Box::new(notebook))
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/ruff_db/src/system/memory_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,17 +463,17 @@ fn not_found() -> std::io::Error {
fn is_a_directory() -> std::io::Error {
// Note: Rust returns `ErrorKind::IsADirectory` for this error but this is a nightly only variant :(.
// So we have to use other for now.
std::io::Error::new(std::io::ErrorKind::Other, "Is a directory")
std::io::Error::other("Is a directory")
}

fn not_a_directory() -> std::io::Error {
// Note: Rust returns `ErrorKind::NotADirectory` for this error but this is a nightly only variant :(.
// So we have to use `Other` for now.
std::io::Error::new(std::io::ErrorKind::Other, "Not a directory")
std::io::Error::other("Not a directory")
}

fn directory_not_empty() -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::Other, "directory not empty")
std::io::Error::other("directory not empty")
}

fn create_dir_all(
Expand Down
22 changes: 11 additions & 11 deletions crates/ruff_formatter/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1388,22 +1388,22 @@ pub fn soft_space_or_block_indent<Context>(content: &impl Format<Context>) -> Bl
pub fn group<Context>(content: &impl Format<Context>) -> Group<Context> {
Group {
content: Argument::new(content),
group_id: None,
id: None,
should_expand: false,
}
}

#[derive(Copy, Clone)]
pub struct Group<'a, Context> {
content: Argument<'a, Context>,
group_id: Option<GroupId>,
id: Option<GroupId>,
should_expand: bool,
}

impl<Context> Group<'_, Context> {
#[must_use]
pub fn with_group_id(mut self, group_id: Option<GroupId>) -> Self {
self.group_id = group_id;
pub fn with_id(mut self, group_id: Option<GroupId>) -> Self {
self.id = group_id;
self
}

Expand All @@ -1429,7 +1429,7 @@ impl<Context> Format<Context> for Group<'_, Context> {
};

f.write_element(FormatElement::Tag(StartGroup(
tag::Group::new().with_id(self.group_id).with_mode(mode),
tag::Group::new().with_id(self.id).with_mode(mode),
)));

Arguments::from(&self.content).fmt(f)?;
Expand All @@ -1443,7 +1443,7 @@ impl<Context> Format<Context> for Group<'_, Context> {
impl<Context> std::fmt::Debug for Group<'_, Context> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Group")
.field("group_id", &self.group_id)
.field("id", &self.id)
.field("should_expand", &self.should_expand)
.field("content", &"{{content}}")
.finish()
Expand Down Expand Up @@ -1642,7 +1642,7 @@ impl<Context> std::fmt::Debug for BestFitParenthesize<'_, Context> {
/// soft_line_break(),
/// if_group_breaks(&token(")"))
/// ])
/// .with_group_id(Some(parentheses_id))
/// .with_id(Some(parentheses_id))
/// .fmt(f)
/// });
///
Expand Down Expand Up @@ -1991,7 +1991,7 @@ impl<Context> IfGroupBreaks<'_, Context> {
/// })),
/// token("]")
/// ],
/// ).with_group_id(Some(group_id))
/// ).with_id(Some(group_id))
/// ])
/// })])?;
///
Expand Down Expand Up @@ -2046,7 +2046,7 @@ impl<Context> std::fmt::Debug for IfGroupBreaks<'_, Context> {
/// let id = f.group_id("head");
///
/// write!(f, [
/// group(&token("Head")).with_group_id(Some(id)),
/// group(&token("Head")).with_id(Some(id)),
/// if_group_breaks(&indent(&token("indented"))).with_group_id(Some(id)),
/// if_group_fits_on_line(&token("indented")).with_group_id(Some(id))
/// ])
Expand All @@ -2071,7 +2071,7 @@ impl<Context> std::fmt::Debug for IfGroupBreaks<'_, Context> {
/// let group_id = f.group_id("header");
///
/// write!(f, [
/// group(&token("(aLongHeaderThatBreaksForSomeReason) =>")).with_group_id(Some(group_id)),
/// group(&token("(aLongHeaderThatBreaksForSomeReason) =>")).with_id(Some(group_id)),
/// indent_if_group_breaks(&format_args![hard_line_break(), token("a => b")], group_id)
/// ])
/// });
Expand Down Expand Up @@ -2101,7 +2101,7 @@ impl<Context> std::fmt::Debug for IfGroupBreaks<'_, Context> {
/// let group_id = f.group_id("header");
///
/// write!(f, [
/// group(&token("(aLongHeaderThatBreaksForSomeReason) =>")).with_group_id(Some(group_id)),
/// group(&token("(aLongHeaderThatBreaksForSomeReason) =>")).with_id(Some(group_id)),
/// indent_if_group_breaks(&format_args![hard_line_break(), token("a => b")], group_id)
/// ])
/// });
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff_formatter/src/printer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2002,7 +2002,7 @@ two lines`,
token("The referenced group breaks."),
hard_line_break()
])
.with_group_id(Some(group_id)),
.with_id(Some(group_id)),
group(&format_args![
token("This group breaks because:"),
soft_line_break_or_space(),
Expand All @@ -2027,15 +2027,15 @@ two lines`,
write!(
f,
[
group(&token("Group with id-2")).with_group_id(Some(id_2)),
group(&token("Group with id-2")).with_id(Some(id_2)),
hard_line_break()
]
)?;

write!(
f,
[
group(&token("Group with id-1 does not fit on the line because it exceeds the line width of 80 characters by")).with_group_id(Some(id_1)),
group(&token("Group with id-1 does not fit on the line because it exceeds the line width of 80 characters by")).with_id(Some(id_1)),
hard_line_break()
]
)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ mod tests {
#[test_case(Path::new("add_missing_cell_id.ipynb"), true; "add_missing_cell_id")]
fn test_cell_id(path: &Path, has_id: bool) -> Result<()> {
let source_notebook = Notebook::from_path(&notebook_path(path))?;
let source_kind = SourceKind::IpyNotebook(source_notebook);
let source_kind = SourceKind::ipy_notebook(source_notebook);
let (_, transformed) = test_contents(
&source_kind,
path,
Expand Down Expand Up @@ -1231,7 +1231,7 @@ mod tests {
format!("async_comprehension_in_sync_comprehension_notebook_{python_version}");
let path = Path::new("resources/test/fixtures/syntax_errors/async_comprehension.ipynb");
let messages = test_contents_syntax_errors(
&SourceKind::IpyNotebook(Notebook::from_path(path)?),
&SourceKind::ipy_notebook(Notebook::from_path(path)?),
path,
&LinterSettings {
unresolved_target_version: python_version.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn is_explicit_concatenation(expr: &Expr) -> Option<bool> {
.iter()
.map(is_explicit_concatenation)
.collect::<Vec<_>>();
if values.iter().any(|v| *v == Some(true)) {
if values.contains(&Some(true)) {
Some(true)
} else if values.iter().all(|v| *v == Some(false)) {
Some(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ struct StringLiteralDisplay<'a> {
/// The elts from the original AST node representing the display.
/// Each elt is the AST representation of a single string literal
/// element in the display
elts: Cow<'a, Vec<ast::Expr>>,
elts: Cow<'a, [ast::Expr]>,
/// The source-code range of the display as a whole
range: TextRange,
/// What kind of a display is it? A dict, set, list or tuple?
Expand Down
40 changes: 36 additions & 4 deletions crates/ruff_linter/src/source_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,47 @@ use colored::Colorize;
use crate::fs;
use crate::text_helpers::ShowNonprinting;

#[derive(Clone, Debug, PartialEq, is_macro::Is)]
#[derive(Clone, Debug, PartialEq)]
pub enum SourceKind {
/// The source contains Python source code.
Python(String),
/// The source contains a Jupyter notebook.
IpyNotebook(Notebook),
IpyNotebook(Box<Notebook>),
}

impl SourceKind {
pub fn ipy_notebook(notebook: Notebook) -> Self {
SourceKind::IpyNotebook(Box::new(notebook))
}

pub fn as_ipy_notebook(&self) -> Option<&Notebook> {
match self {
SourceKind::IpyNotebook(notebook) => Some(notebook),
SourceKind::Python(_) => None,
}
}

pub fn as_python(&self) -> Option<&str> {
match self {
SourceKind::Python(code) => Some(code),
SourceKind::IpyNotebook(_) => None,
}
}

pub fn expect_python(self) -> String {
match self {
SourceKind::Python(code) => code,
SourceKind::IpyNotebook(_) => panic!("expected python code"),
}
}

pub fn expect_ipy_notebook(self) -> Notebook {
match self {
SourceKind::IpyNotebook(notebook) => *notebook,
SourceKind::Python(_) => panic!("expected ipy notebook"),
}
}

#[must_use]
pub(crate) fn updated(&self, new_source: String, source_map: &SourceMap) -> Self {
match self {
Expand Down Expand Up @@ -52,7 +84,7 @@ impl SourceKind {
let notebook = Notebook::from_path(path)?;
Ok(notebook
.is_python_notebook()
.then_some(Self::IpyNotebook(notebook)))
.then_some(Self::IpyNotebook(Box::new(notebook))))
} else {
let contents = std::fs::read_to_string(path)?;
Ok(Some(Self::Python(contents)))
Expand All @@ -69,7 +101,7 @@ impl SourceKind {
let notebook = Notebook::from_source_code(&source_code)?;
Ok(notebook
.is_python_notebook()
.then_some(Self::IpyNotebook(notebook)))
.then_some(Self::IpyNotebook(Box::new(notebook))))
} else {
Ok(Some(Self::Python(source_code)))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub(crate) fn assert_notebook_path(
) -> Result<TestedNotebook, NotebookError> {
let source_notebook = Notebook::from_path(path.as_ref())?;

let source_kind = SourceKind::IpyNotebook(source_notebook);
let source_kind = SourceKind::ipy_notebook(source_notebook);
let (messages, transformed) = test_contents(&source_kind, path.as_ref(), settings);
let expected_notebook = Notebook::from_path(expected.as_ref())?;
let linted_notebook = transformed.into_owned().expect_ipy_notebook();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl<'ast> Format<PyFormatContext<'ast>> for FormatOptionalParentheses<'_, 'ast>
soft_line_break(),
if_group_breaks(&token(")"))
])
.with_group_id(Some(parens_id))]
.with_id(Some(parens_id))]
)
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/ruff_python_formatter/src/statement/stmt_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ impl Format<PyFormatContext<'_>> for FormatStatementsLastExpression<'_> {
soft_block_indent(&format_args![flat, inline_comments]),
token(")"),
])
.with_group_id(Some(group_id))
.with_id(Some(group_id))
.should_expand(true)
.fmt(f)
});
Expand All @@ -433,7 +433,7 @@ impl Format<PyFormatContext<'_>> for FormatStatementsLastExpression<'_> {
token(")"),
inline_comments,
])
.with_group_id(Some(group_id))
.with_id(Some(group_id))
.should_expand(true)
.fmt(f)
});
Expand Down Expand Up @@ -501,7 +501,7 @@ impl Format<PyFormatContext<'_>> for FormatStatementsLastExpression<'_> {
soft_block_indent(&format_args![f_string_flat, inline_comments]),
token(")"),
])
.with_group_id(Some(group_id))
.with_id(Some(group_id))
.should_expand(true)
.fmt(f)
});
Expand Down Expand Up @@ -817,7 +817,7 @@ impl Format<PyFormatContext<'_>> for FormatStatementsLastExpression<'_> {
space(),
token("("),
group(&soft_block_indent(&format_expanded))
.with_group_id(Some(group_id))
.with_id(Some(group_id))
.should_expand(true),
token(")"),
inline_comments
Expand Down Expand Up @@ -875,7 +875,7 @@ impl Format<PyFormatContext<'_>> for FormatStatementsLastExpression<'_> {
space(),
token("("),
group(&soft_block_indent(&format_expanded))
.with_group_id(Some(group_id))
.with_id(Some(group_id))
.should_expand(true),
token(")"),
inline_comments
Expand Down
8 changes: 2 additions & 6 deletions crates/ruff_python_semantic/src/analyze/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,7 @@ pub fn is_immutable_annotation(
.is_some_and(|qualified_name| {
is_immutable_non_generic_type(qualified_name.segments())
|| is_immutable_generic_type(qualified_name.segments())
|| extend_immutable_calls
.iter()
.any(|target| qualified_name == *target)
|| extend_immutable_calls.contains(&qualified_name)
})
}
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => semantic
Expand Down Expand Up @@ -308,9 +306,7 @@ pub fn is_immutable_func(
.resolve_qualified_name(map_subscript(func))
.is_some_and(|qualified_name| {
is_immutable_return_type(qualified_name.segments())
|| extend_immutable_calls
.iter()
.any(|target| qualified_name == *target)
|| extend_immutable_calls.contains(&qualified_name)
})
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_server/src/session/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ impl DocumentQuery {
ruff_linter::source_kind::SourceKind::Python(document.contents().to_string())
}
Self::Notebook { notebook, .. } => {
ruff_linter::source_kind::SourceKind::IpyNotebook(notebook.make_ruff_notebook())
ruff_linter::source_kind::SourceKind::ipy_notebook(notebook.make_ruff_notebook())
}
}
}
Expand Down
Loading
Loading