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

Fix SIM118 auto-fix #3695

Merged
merged 1 commit into from
Mar 23, 2023
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
2 changes: 2 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_simplify/SIM118.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
{k: k for k in obj.keys()} # SIM118

(k for k in obj.keys()) # SIM118

key in (obj or {}).keys() # SIM118
43 changes: 38 additions & 5 deletions crates/ruff/src/rules/flake8_simplify/rules/key_in_dict.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use anyhow::Result;
use libcst_native::{Codegen, CodegenState};
use log::error;
use rustpython_parser::ast::{Cmpop, Expr, ExprKind};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
use ruff_diagnostics::Fix;
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::source_code::{Locator, Stylist};
use ruff_python_ast::types::Range;

use crate::checkers::ast::Checker;
use crate::cst::matchers::{match_attribute, match_call, match_expression};
use crate::registry::AsRule;

#[violation]
Expand All @@ -26,6 +32,26 @@ impl AlwaysAutofixableViolation for InDictKeys {
}
}

fn get_value_content_for_key_in_dict(
locator: &Locator,
stylist: &Stylist,
expr: &rustpython_parser::ast::Expr,
) -> Result<String> {
let content = locator.slice(expr);
let mut expression = match_expression(content)?;
let call = match_call(&mut expression)?;
let attribute = match_attribute(&mut call.func)?;

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
attribute.value.codegen(&mut state);

Ok(state.to_string())
}

/// SIM118
fn key_in_dict(checker: &mut Checker, left: &Expr, right: &Expr, range: Range) {
let ExprKind::Call {
Expand All @@ -39,7 +65,7 @@ fn key_in_dict(checker: &mut Checker, left: &Expr, right: &Expr, range: Range) {
return;
}

let ExprKind::Attribute { attr, value, .. } = &func.node else {
let ExprKind::Attribute { attr, .. } = &func.node else {
return;
};
if attr != "keys" {
Expand All @@ -48,18 +74,25 @@ fn key_in_dict(checker: &mut Checker, left: &Expr, right: &Expr, range: Range) {

// Slice exact content to preserve formatting.
let left_content = checker.locator.slice(left);
let value_content = checker.locator.slice(value);
let value_content =
match get_value_content_for_key_in_dict(checker.locator, checker.stylist, right) {
Ok(value_content) => value_content,
Err(err) => {
error!("Failed to get value content for key in dict: {}", err);
return;
}
};

let mut diagnostic = Diagnostic::new(
InDictKeys {
key: left_content.to_string(),
dict: value_content.to_string(),
dict: value_content.clone(),
},
range,
);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement(
value_content.to_string(),
value_content,
right.location,
right.end_location.unwrap(),
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,24 @@ expression: diagnostics
row: 22
column: 22
parent: ~
- kind:
name: InDictKeys
body: "Use `key in (obj or {})` instead of `key in (obj or {}).keys()`"
suggestion: "Convert to `key in (obj or {})`"
fixable: true
location:
row: 24
column: 0
end_location:
row: 24
column: 25
fix:
content: "(obj or {})"
location:
row: 24
column: 7
end_location:
row: 24
column: 25
parent: ~