Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Rust to v1.77 #10510

Merged
merged 2 commits into from Mar 21, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/ruff/src/commands/check.rs
Expand Up @@ -252,6 +252,7 @@ mod test {
for file in [&pyproject_toml, &python_file, &notebook] {
fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.mode(0o000)
.open(file)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/panic.rs
Expand Up @@ -16,7 +16,7 @@ impl std::fmt::Display for PanicError {
}

thread_local! {
static LAST_PANIC: std::cell::Cell<Option<PanicError>> = std::cell::Cell::new(None);
static LAST_PANIC: std::cell::Cell<Option<PanicError>> = const { std::cell::Cell::new(None) };
}

/// [`catch_unwind`](std::panic::catch_unwind) wrapper that sets a custom [`set_hook`](std::panic::set_hook)
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/tests/integration_test.rs
Expand Up @@ -1353,6 +1353,7 @@ fn unreadable_pyproject_toml() -> Result<()> {
// Create an empty file with 000 permissions
fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.mode(0o000)
.open(pyproject_toml)?;
Expand Down
7 changes: 1 addition & 6 deletions crates/ruff_linter/src/checkers/imports.rs
Expand Up @@ -20,12 +20,7 @@ use crate::rules::isort::block::{Block, BlockBuilder};
use crate::settings::LinterSettings;

fn extract_import_map(path: &Path, package: Option<&Path>, blocks: &[&Block]) -> Option<ImportMap> {
let Some(package) = package else {
return None;
};
let Some(module_path) = to_module_path(package, path) else {
return None;
};
let module_path = to_module_path(package?, path)?;

let num_imports = blocks.iter().map(|block| block.imports.len()).sum();
let mut module_imports = Vec::with_capacity(num_imports);
Expand Down
5 changes: 1 addition & 4 deletions crates/ruff_linter/src/fix/edits.rs
Expand Up @@ -40,10 +40,7 @@ pub(crate) fn delete_stmt(
locator: &Locator,
indexer: &Indexer,
) -> Edit {
if parent
.map(|parent| is_lone_child(stmt, parent))
.unwrap_or_default()
{
if parent.is_some_and(|parent| is_lone_child(stmt, parent)) {
// If removing this node would lead to an invalid syntax tree, replace
// it with a `pass`.
Edit::range_replacement("pass".to_string(), stmt.range())
Expand Down
4 changes: 1 addition & 3 deletions crates/ruff_linter/src/importer/insertion.rs
Expand Up @@ -278,9 +278,7 @@ impl<'a> Insertion<'a> {
/// Find the end of the last docstring.
fn match_docstring_end(body: &[Stmt]) -> Option<TextSize> {
let mut iter = body.iter();
let Some(mut stmt) = iter.next() else {
return None;
};
let mut stmt = iter.next()?;
if !is_docstring_stmt(stmt) {
return None;
}
Expand Down
Expand Up @@ -80,9 +80,7 @@ pub(crate) fn compare_to_hardcoded_password_string(
.diagnostics
.extend(comparators.iter().filter_map(|comp| {
string_literal(comp).filter(|string| !string.is_empty())?;
let Some(name) = password_target(left) else {
return None;
};
let name = password_target(left)?;
Some(Diagnostic::new(
HardcodedPasswordString {
name: name.to_string(),
Expand Down
Expand Up @@ -159,9 +159,7 @@ fn get_element_type(element: &Stmt, semantic: &SemanticModel) -> Option<ContentT
return Some(ContentType::FieldDeclaration);
}
}
let Some(expr) = targets.first() else {
return None;
};
let expr = targets.first()?;
let Expr::Name(ast::ExprName { id, .. }) = expr else {
return None;
};
Expand Down
Expand Up @@ -61,15 +61,9 @@ pub(crate) fn unconventional_import_alias(
binding: &Binding,
conventions: &FxHashMap<String, String>,
) -> Option<Diagnostic> {
let Some(import) = binding.as_any_import() else {
return None;
};

let import = binding.as_any_import()?;
let qualified_name = import.qualified_name().to_string();

let Some(expected_alias) = conventions.get(qualified_name.as_str()) else {
return None;
};
let expected_alias = conventions.get(qualified_name.as_str())?;

let name = binding.name(checker.locator());
if binding.is_alias() && name == expected_alias {
Expand Down
Expand Up @@ -148,10 +148,7 @@ fn match_builtin_type(expr: &Expr, semantic: &SemanticModel) -> Option<ExprType>
/// Return the [`ExprType`] of an [`Expr`] if it is a literal (e.g., an `int`, like `1`, or a
/// `bool`, like `True`).
fn match_literal_type(expr: &Expr) -> Option<ExprType> {
let Some(literal_expr) = expr.as_literal_expr() else {
return None;
};
let result = match literal_expr {
Some(match expr.as_literal_expr()? {
LiteralExpressionRef::BooleanLiteral(_) => ExprType::Bool,
LiteralExpressionRef::StringLiteral(_) => ExprType::Str,
LiteralExpressionRef::BytesLiteral(_) => ExprType::Bytes,
Expand All @@ -163,6 +160,5 @@ fn match_literal_type(expr: &Expr) -> Option<ExprType> {
LiteralExpressionRef::NoneLiteral(_) | LiteralExpressionRef::EllipsisLiteral(_) => {
return None;
}
};
Some(result)
})
}
Expand Up @@ -8,9 +8,7 @@ pub(super) fn get_mark_decorators(
decorators: &[Decorator],
) -> impl Iterator<Item = (&Decorator, &str)> {
decorators.iter().filter_map(|decorator| {
let Some(name) = UnqualifiedName::from_expr(map_callable(&decorator.expression)) else {
return None;
};
let name = UnqualifiedName::from_expr(map_callable(&decorator.expression))?;
let ["pytest", "mark", marker] = name.segments() else {
return None;
};
Expand Down
Expand Up @@ -541,7 +541,7 @@ pub(crate) fn compare_with_tuple(checker: &mut Checker, expr: &Expr) {

// Create a `x in (a, b)` expression.
let node = ast::ExprTuple {
elts: comparators.into_iter().map(Clone::clone).collect(),
elts: comparators.into_iter().cloned().collect(),
ctx: ExprContext::Load,
range: TextRange::default(),
parenthesized: true,
Expand Down
Expand Up @@ -274,10 +274,11 @@ fn match_loop(stmt: &Stmt) -> Option<Loop> {
if !nested_elif_else_clauses.is_empty() {
return None;
}
let [Stmt::Return(ast::StmtReturn { value, range: _ })] = nested_body.as_slice() else {
return None;
};
let Some(value) = value else {
let [Stmt::Return(ast::StmtReturn {
value: Some(value),
range: _,
})] = nested_body.as_slice()
else {
return None;
};
let Expr::BooleanLiteral(ast::ExprBooleanLiteral { value, .. }) = value.as_ref() else {
Expand Down
Expand Up @@ -82,9 +82,7 @@ fn fix_banned_relative_import(
generator: Generator,
) -> Option<Fix> {
// Only fix is the module path is known.
let Some(module_path) = resolve_imported_module_path(level, module, module_path) else {
return None;
};
let module_path = resolve_imported_module_path(level, module, module_path)?;

// Require import to be a valid module:
// https://python.org/dev/peps/pep-0008/#package-and-module-names
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/isort/categorize.rs
Expand Up @@ -80,10 +80,12 @@ enum Reason<'a> {
Future,
KnownStandardLibrary,
SamePackage,
#[allow(dead_code)]
SourceMatch(&'a Path),
NoMatch,
UserDefinedSection,
NoSections,
#[allow(dead_code)]
DisabledSection(&'a ImportSection),
}

Expand Down
4 changes: 1 addition & 3 deletions crates/ruff_linter/src/rules/isort/sorting.rs
Expand Up @@ -103,9 +103,7 @@ impl<'a> ModuleKey<'a> {
) -> Self {
let level = level.unwrap_or_default();

let force_to_top = !name
.map(|name| settings.force_to_top.contains(name))
.unwrap_or_default(); // `false` < `true` so we get forced to top first
let force_to_top = !name.is_some_and(|name| settings.force_to_top.contains(name)); // `false` < `true` so we get forced to top first

let maybe_length = (settings.length_sort
|| (settings.length_sort_straight && style == ImportStyle::Straight))
Expand Down
Expand Up @@ -87,10 +87,7 @@ pub(crate) fn doc_line_too_long(
indexer: &Indexer,
settings: &LinterSettings,
) -> Option<Diagnostic> {
let Some(limit) = settings.pycodestyle.max_doc_length else {
return None;
};

let limit = settings.pycodestyle.max_doc_length?;
Overlong::try_from_line(
line,
indexer,
Expand Down
13 changes: 3 additions & 10 deletions crates/ruff_linter/src/rules/pylint/rules/import_self.rs
Expand Up @@ -35,9 +35,7 @@ impl Violation for ImportSelf {

/// PLW0406
pub(crate) fn import_self(alias: &Alias, module_path: Option<&[String]>) -> Option<Diagnostic> {
let Some(module_path) = module_path else {
return None;
};
let module_path = module_path?;

if alias.name.split('.').eq(module_path) {
return Some(Diagnostic::new(
Expand All @@ -58,13 +56,8 @@ pub(crate) fn import_from_self(
names: &[Alias],
module_path: Option<&[String]>,
) -> Option<Diagnostic> {
let Some(module_path) = module_path else {
return None;
};
let Some(imported_module_path) = resolve_imported_module_path(level, module, Some(module_path))
else {
return None;
};
let module_path = module_path?;
let imported_module_path = resolve_imported_module_path(level, module, Some(module_path))?;

if imported_module_path
.split('.')
Expand Down
4 changes: 1 addition & 3 deletions crates/ruff_linter/src/rules/pyupgrade/rules/f_strings.rs
Expand Up @@ -86,9 +86,7 @@ impl<'a> FormatSummaryValues<'a> {
value,
range: _,
} = keyword;
let Some(key) = arg else {
return None;
};
let key = arg.as_ref()?;
if contains_quotes(locator.slice(value)) || locator.contains_line_break(value.range()) {
return None;
}
Expand Down
Expand Up @@ -260,9 +260,7 @@ fn clean_params_dictionary(right: &Expr, locator: &Locator, stylist: &Stylist) -
}
contents.push('(');
if is_multi_line {
let Some(indent) = indent else {
return None;
};
let indent = indent?;

for item in &arguments {
contents.push_str(stylist.line_ending().as_str());
Expand Down
Expand Up @@ -127,8 +127,7 @@ impl<'src, 'loc> UselessSuppressionComments<'src, 'loc> {
// check if the comment is inside of an expression.
if comment
.enclosing
.map(|n| !is_valid_enclosing_node(n))
.unwrap_or_default()
.is_some_and(|n| !is_valid_enclosing_node(n))
{
return Err(IgnoredReason::InNonStatement);
}
Expand Down
Expand Up @@ -83,12 +83,7 @@ fn match_slice_info(expr: &Expr) -> Option<SliceInfo> {
else {
return None;
};

let Some(slice_start) = int.as_i32() else {
return None;
};

Some(slice_start)
Some(int.as_i32()?)
} else {
None
};
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/test.rs
Expand Up @@ -92,7 +92,7 @@ pub fn test_snippet(contents: &str, settings: &LinterSettings) -> Vec<Message> {
}

thread_local! {
static MAX_ITERATIONS: std::cell::Cell<usize> = std::cell::Cell::new(8);
static MAX_ITERATIONS: std::cell::Cell<usize> = const { std::cell::Cell::new(8) };
}

pub fn set_max_iterations(max: usize) {
Expand Down
4 changes: 1 addition & 3 deletions crates/ruff_python_ast/src/helpers.rs
Expand Up @@ -878,9 +878,7 @@ pub fn resolve_imported_module_path<'a>(
return Some(Cow::Borrowed(module.unwrap_or("")));
}

let Some(module_path) = module_path else {
return None;
};
let module_path = module_path?;

if level as usize >= module_path.len() {
return None;
Expand Down
Expand Up @@ -175,9 +175,7 @@ fn find_double_star(pattern: &PatternMatchMapping, source: &str) -> Option<(Text
} = pattern;

// If there's no `rest` element, there's no `**`.
let Some(rest) = rest else {
return None;
};
let rest = rest.as_ref()?;

let mut tokenizer =
SimpleTokenizer::starts_at(patterns.last().map_or(pattern.start(), Ranged::end), source);
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
@@ -1,2 +1,2 @@
[toolchain]
channel = "1.76"
channel = "1.77"