Skip to content

Commit

Permalink
Avoid rendering display-only rules as fixable (#9649)
Browse files Browse the repository at this point in the history
Closes #9505.

The `ERA` rule is no longer marked as fixable:

![Screenshot 2024-01-26 at 9 17
48 AM](https://github.com/astral-sh/ruff/assets/1309177/fdc6217f-38ff-4098-b6ca-37ff51b710ab)
  • Loading branch information
charliermarsh committed Jan 26, 2024
1 parent 91046e4 commit 79a0ddc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_index::Indexer;
use ruff_source_file::Locator;
Expand Down Expand Up @@ -30,14 +30,16 @@ use super::super::detection::comment_contains_code;
#[violation]
pub struct CommentedOutCode;

impl AlwaysFixableViolation for CommentedOutCode {
impl Violation for CommentedOutCode {
const FIX_AVAILABILITY: FixAvailability = FixAvailability::None;

#[derive_message_formats]
fn message(&self) -> String {
format!("Found commented-out code")
}

fn fix_title(&self) -> String {
"Remove commented-out code".to_string()
fn fix_title(&self) -> Option<String> {
Some(format!("Remove commented-out code"))
}
}

Expand Down Expand Up @@ -65,7 +67,6 @@ pub(crate) fn commented_out_code(
// Verify that the comment is on its own line, and that it contains code.
if is_standalone_comment(line) && comment_contains_code(line, &settings.task_tags[..]) {
let mut diagnostic = Diagnostic::new(CommentedOutCode, *range);

diagnostic.set_fix(Fix::display_only_edit(Edit::range_deletion(
locator.full_lines_range(*range),
)));
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use anyhow::Result;
use itertools::Itertools;
use rustc_hash::FxHashMap;

use ruff_diagnostics::{Diagnostic, FixAvailability};
use ruff_diagnostics::{Applicability, Diagnostic, FixAvailability};
use ruff_python_ast::PySourceType;
use ruff_python_codegen::Stylist;
use ruff_python_index::Indexer;
Expand Down Expand Up @@ -241,7 +241,7 @@ Source with applied fixes:
.into_iter()
.map(|diagnostic| {
let rule = diagnostic.kind.rule();
let fixable = diagnostic.fix.is_some();
let fixable = diagnostic.fix.as_ref().is_some_and(|fix| matches!(fix.applicability(), Applicability::Safe | Applicability::Unsafe));

match (fixable, rule.fixable()) {
(true, FixAvailability::Sometimes | FixAvailability::Always)
Expand Down

0 comments on commit 79a0ddc

Please sign in to comment.