Skip to content

Commit

Permalink
Remove safety from fix constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Oct 5, 2023
1 parent 9eb609c commit 967a209
Show file tree
Hide file tree
Showing 177 changed files with 321 additions and 307 deletions.
12 changes: 6 additions & 6 deletions crates/ruff_diagnostics/src/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct Fix {

impl Fix {
/// Create a new [`Fix`] with [safe applicability](Applicability::Always) from an [`Edit`] element.
pub fn always_safe(edit: Edit) -> Self {
pub fn always_applies(edit: Edit) -> Self {
Self {
edits: vec![edit],
applicability: Applicability::Always,
Expand All @@ -57,7 +57,7 @@ impl Fix {
}

/// Create a new [`Fix`] with [safe applicability](Applicability::Always) from multiple [`Edit`] elements.
pub fn always_safe_edits(edit: Edit, rest: impl IntoIterator<Item = Edit>) -> Self {
pub fn always_applies_edits(edit: Edit, rest: impl IntoIterator<Item = Edit>) -> Self {
let mut edits: Vec<Edit> = std::iter::once(edit).chain(rest).collect();
edits.sort_by_key(|edit| (edit.start(), edit.end()));
Self {
Expand All @@ -68,7 +68,7 @@ impl Fix {
}

/// Create a new [`Fix`] with [unsafe applicability](Applicable::Sometimes) from an [`Edit`] element.
pub fn sometimes_safe(edit: Edit) -> Self {
pub fn sometimes_applies(edit: Edit) -> Self {
Self {
edits: vec![edit],
applicability: Applicability::Sometimes,
Expand All @@ -77,7 +77,7 @@ impl Fix {
}

/// Create a new [`Fix`] with [unsafe applicability](Applicability::Sometimes) from multiple [`Edit`] elements.
pub fn sometimes_safe_edits(edit: Edit, rest: impl IntoIterator<Item = Edit>) -> Self {
pub fn sometimes_applies_edits(edit: Edit, rest: impl IntoIterator<Item = Edit>) -> Self {
let mut edits: Vec<Edit> = std::iter::once(edit).chain(rest).collect();
edits.sort_by_key(|edit| (edit.start(), edit.end()));
Self {
Expand All @@ -88,7 +88,7 @@ impl Fix {
}

/// Create a new [`Fix`] with [manual applicability](Applicability::Never) from an [`Edit`] element.
pub fn never_safe(edit: Edit) -> Self {
pub fn never_applies(edit: Edit) -> Self {
Self {
edits: vec![edit],
applicability: Applicability::Never,
Expand All @@ -97,7 +97,7 @@ impl Fix {
}

/// Create a new [`Fix`] with [manual applicability](Applicability::Never) from multiple [`Edit`] elements.
pub fn never_safe_edits(edit: Edit, rest: impl IntoIterator<Item = Edit>) -> Self {
pub fn never_applies_edits(edit: Edit, rest: impl IntoIterator<Item = Edit>) -> Self {
let mut edits: Vec<Edit> = std::iter::once(edit).chain(rest).collect();
edits.sort_by_key(|edit| (edit.start(), edit.end()));
Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/checkers/ast/analyze/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(crate) fn bindings(checker: &mut Checker) {
binding,
checker.locator,
)
.map(Fix::always_safe)
.map(Fix::always_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
14 changes: 8 additions & 6 deletions crates/ruff_linter/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub(crate) fn check_noqa(
let mut diagnostic =
Diagnostic::new(UnusedNOQA { codes: None }, directive.range());
if settings.rules.should_fix(diagnostic.kind.rule()) {
diagnostic.set_fix(Fix::sometimes_safe(delete_noqa(
diagnostic.set_fix(Fix::sometimes_applies(delete_noqa(
directive.range(),
locator,
)));
Expand Down Expand Up @@ -177,15 +177,17 @@ pub(crate) fn check_noqa(
);
if settings.rules.should_fix(diagnostic.kind.rule()) {
if valid_codes.is_empty() {
diagnostic.set_fix(Fix::sometimes_safe(delete_noqa(
diagnostic.set_fix(Fix::sometimes_applies(delete_noqa(
directive.range(),
locator,
)));
} else {
diagnostic.set_fix(Fix::sometimes_safe(Edit::range_replacement(
format!("# noqa: {}", valid_codes.join(", ")),
directive.range(),
)));
diagnostic.set_fix(Fix::sometimes_applies(
Edit::range_replacement(
format!("# noqa: {}", valid_codes.join(", ")),
directive.range(),
),
));
}
}
diagnostics.push(diagnostic);
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/fix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ mod tests {
// The choice of rule here is arbitrary.
kind: MissingNewlineAtEndOfFile.into(),
range: edit.range(),
fix: Some(Fix::always_safe(edit)),
fix: Some(Fix::always_applies(edit)),
parent: None,
})
.collect()
Expand Down
9 changes: 4 additions & 5 deletions crates/ruff_linter/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,9 @@ def fibonacci(n):
},
TextRange::new(TextSize::from(7), TextSize::from(9)),
)
.with_fix(Fix::sometimes_safe(Edit::range_deletion(TextRange::new(
TextSize::from(0),
TextSize::from(10),
))));
.with_fix(Fix::sometimes_applies(Edit::range_deletion(
TextRange::new(TextSize::from(0), TextSize::from(10)),
)));

let fib_source = SourceFileBuilder::new("fib.py", fib).finish();

Expand All @@ -193,7 +192,7 @@ def fibonacci(n):
},
TextRange::new(TextSize::from(94), TextSize::from(95)),
)
.with_fix(Fix::sometimes_safe(Edit::deletion(
.with_fix(Fix::sometimes_applies(Edit::deletion(
TextSize::from(94),
TextSize::from(99),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub(crate) fn commented_out_code(
let mut diagnostic = Diagnostic::new(CommentedOutCode, *range);

if settings.rules.should_fix(Rule::CommentedOutCode) {
diagnostic.set_fix(Fix::never_safe(Edit::range_deletion(
diagnostic.set_fix(Fix::never_applies(Edit::range_deletion(
locator.full_lines_range(*range),
)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ pub(crate) fn definition(
function.identifier(),
);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Fix::sometimes_safe(Edit::insertion(
diagnostic.set_fix(Fix::sometimes_applies(Edit::insertion(
" -> None".to_string(),
function.parameters.range().end(),
)));
Expand All @@ -721,7 +721,7 @@ pub(crate) fn definition(
);
if checker.patch(diagnostic.kind.rule()) {
if let Some(return_type) = simple_magic_return_type(name) {
diagnostic.set_fix(Fix::sometimes_safe(Edit::insertion(
diagnostic.set_fix(Fix::sometimes_applies(Edit::insertion(
format!(" -> {return_type}"),
function.parameters.range().end(),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub(crate) fn assert_false(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg:

let mut diagnostic = Diagnostic::new(AssertFalse, test.range());
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Fix::sometimes_safe(Edit::range_replacement(
diagnostic.set_fix(Fix::sometimes_applies(Edit::range_replacement(
checker.generator().stmt(&assertion_error(msg)),
stmt.range(),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn duplicate_handler_exceptions<'a>(
expr.range(),
);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Fix::always_safe(Edit::range_replacement(
diagnostic.set_fix(Fix::always_applies(Edit::range_replacement(
// Single exceptions don't require parentheses, but since we're _removing_
// parentheses, insert whitespace as needed.
if let [elt] = unique_elts.as_slice() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub(crate) fn getattr_with_constant(

let mut diagnostic = Diagnostic::new(GetAttrWithConstant, expr.range());
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Fix::sometimes_safe(Edit::range_replacement(
diagnostic.set_fix(Fix::sometimes_applies(Edit::range_replacement(
pad(
if matches!(
obj,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,8 @@ fn move_initialization(
}

let initialization_edit = Edit::insertion(content, pos);
Some(Fix::never_safe_edits(default_edit, [initialization_edit]))
Some(Fix::never_applies_edits(
default_edit,
[initialization_edit],
))
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub(crate) fn redundant_tuple_in_exception_handler(
// ```
// Otherwise, the output will be invalid syntax, since we're removing a set of
// parentheses.
diagnostic.set_fix(Fix::always_safe(Edit::range_replacement(
diagnostic.set_fix(Fix::always_applies(Edit::range_replacement(
pad(
checker.generator().expr(elt),
type_.range(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub(crate) fn setattr_with_constant(
if expr == child.as_ref() {
let mut diagnostic = Diagnostic::new(SetAttrWithConstant, expr.range());
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Fix::sometimes_safe(Edit::range_replacement(
diagnostic.set_fix(Fix::sometimes_applies(Edit::range_replacement(
assignment(obj, name, value, checker.generator()),
expr.range(),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub(crate) fn unreliable_callable_check(
if checker.patch(diagnostic.kind.rule()) {
if id == "hasattr" {
if checker.semantic().is_builtin("callable") {
diagnostic.set_fix(Fix::always_safe(Edit::range_replacement(
diagnostic.set_fix(Fix::always_applies(Edit::range_replacement(
format!("callable({})", checker.locator().slice(obj)),
expr.range(),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub(crate) fn unused_loop_control_variable(checker: &mut Checker, stmt_for: &ast
.filter(|binding| binding.start() >= expr.start())
.all(|binding| !binding.is_used())
{
diagnostic.set_fix(Fix::sometimes_safe(Edit::range_replacement(
diagnostic.set_fix(Fix::sometimes_applies(Edit::range_replacement(
rename,
expr.range(),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,9 @@ pub(crate) fn trailing_commas(
let comma = prev.spanned.unwrap();
let mut diagnostic = Diagnostic::new(ProhibitedTrailingComma, comma.1);
if settings.rules.should_fix(Rule::ProhibitedTrailingComma) {
diagnostic.set_fix(Fix::always_safe(Edit::range_deletion(diagnostic.range())));
diagnostic.set_fix(Fix::always_applies(Edit::range_deletion(
diagnostic.range(),
)));
}
diagnostics.push(diagnostic);
}
Expand Down Expand Up @@ -365,7 +367,7 @@ pub(crate) fn trailing_commas(
// removing any brackets in the same linter pass - doing both at the same time could
// lead to a syntax error.
let contents = locator.slice(missing_comma.1);
diagnostic.set_fix(Fix::always_safe(Edit::range_replacement(
diagnostic.set_fix(Fix::always_applies(Edit::range_replacement(
format!("{contents},"),
missing_comma.1,
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ pub(crate) fn fix_unnecessary_comprehension_any_all(
_ => whitespace_after_arg,
};

Ok(Fix::sometimes_safe(Edit::range_replacement(
Ok(Fix::sometimes_applies(Edit::range_replacement(
tree.codegen_stylist(stylist),
expr.range(),
)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ pub(crate) fn unnecessary_call_around_sorted(
checker.stylist(),
)?;
if outer.id == "reversed" {
Ok(Fix::sometimes_safe(edit))
Ok(Fix::sometimes_applies(edit))
} else {
Ok(Fix::always_safe(edit))
Ok(Fix::always_applies(edit))
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub(crate) fn unnecessary_collection_call(
);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_collection_call(expr, checker).map(Fix::sometimes_safe)
fixes::fix_unnecessary_collection_call(expr, checker).map(Fix::sometimes_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn add_diagnostic(checker: &mut Checker, expr: &Expr) {
if checker.patch(diagnostic.kind.rule()) {
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_comprehension(expr, checker.locator(), checker.stylist())
.map(Fix::sometimes_safe)
.map(Fix::sometimes_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub(crate) fn unnecessary_double_cast_or_process(
checker.locator(),
checker.stylist(),
)
.map(Fix::sometimes_safe)
.map(Fix::sometimes_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub(crate) fn unnecessary_generator_dict(
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorDict, expr.range());
if checker.patch(diagnostic.kind.rule()) {
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_generator_dict(expr, checker).map(Fix::sometimes_safe)
fixes::fix_unnecessary_generator_dict(expr, checker).map(Fix::sometimes_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(crate) fn unnecessary_generator_list(
if checker.patch(diagnostic.kind.rule()) {
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_generator_list(expr, checker.locator(), checker.stylist())
.map(Fix::sometimes_safe)
.map(Fix::sometimes_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub(crate) fn unnecessary_generator_set(
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorSet, expr.range());
if checker.patch(diagnostic.kind.rule()) {
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_generator_set(expr, checker).map(Fix::sometimes_safe)
fixes::fix_unnecessary_generator_set(expr, checker).map(Fix::sometimes_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) fn unnecessary_list_call(
if checker.patch(diagnostic.kind.rule()) {
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_list_call(expr, checker.locator(), checker.stylist())
.map(Fix::sometimes_safe)
.map(Fix::sometimes_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ pub(crate) fn unnecessary_list_comprehension_dict(
let mut diagnostic = Diagnostic::new(UnnecessaryListComprehensionDict, expr.range());
if checker.patch(diagnostic.kind.rule()) {
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_list_comprehension_dict(expr, checker).map(Fix::sometimes_safe)
fixes::fix_unnecessary_list_comprehension_dict(expr, checker)
.map(Fix::sometimes_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub(crate) fn unnecessary_list_comprehension_set(
if checker.patch(diagnostic.kind.rule()) {
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_list_comprehension_set(expr, checker)
.map(Fix::sometimes_safe)
.map(Fix::sometimes_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub(crate) fn unnecessary_literal_dict(
);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_literal_dict(expr, checker).map(Fix::sometimes_safe)
fixes::fix_unnecessary_literal_dict(expr, checker).map(Fix::sometimes_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub(crate) fn unnecessary_literal_set(
);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_literal_set(expr, checker).map(Fix::sometimes_safe)
fixes::fix_unnecessary_literal_set(expr, checker).map(Fix::sometimes_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub(crate) fn unnecessary_literal_within_dict_call(
checker.locator(),
checker.stylist(),
)
.map(Fix::sometimes_safe)
.map(Fix::sometimes_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub(crate) fn unnecessary_literal_within_list_call(
checker.locator(),
checker.stylist(),
)
.map(Fix::sometimes_safe)
.map(Fix::sometimes_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub(crate) fn unnecessary_literal_within_tuple_call(
checker.locator(),
checker.stylist(),
)
.map(Fix::sometimes_safe)
.map(Fix::sometimes_applies)
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Loading

0 comments on commit 967a209

Please sign in to comment.