Repository: astral-sh/ruff
File: crates/ruff_linter/src/rules/pyupgrade/rules/quoted_annotation.rs (Rust)

Code before:
    fn message(&self) -> String {
        "Remove quotes from type annotation".to_string()
    }

    fn fix_title(&self) -> String {
        "Remove quotes".to_string()
    }
}

/// UP037
pub(crate) fn quoted_annotation(checker: &Checker, annotation: &str, range: TextRange) {
    let add_future_import = checker.settings().future_annotations
        && checker.semantic().in_runtime_evaluated_annotation();

    if !(checker.semantic().in_typing_only_annotation() || add_future_import) {
        return;
    }

    let placeholder_range = TextRange::up_to(annotation.text_len());
    let spans_multiple_lines = annotation.contains_line_break(placeholder_range);

    let last_token_is_comment = checker
        .tokens()
        .iter()
        .rev()
        .find(|tok| !tok.kind().is_any_newline())
        .is_some_and(|tok| tok.kind() == TokenKind::Comment);

    let new_content = match (spans_multiple_lines, last_token_is_comment) {
        (_, false) if in_parameter_annotation(range.start(), checker.semantic()) => {
            annotation.to_string()
        }
        (false, false) => annotation.to_string(),
        (true, false) => format!("({annotation})"),
        (_, true) => format!("({annotation}\n)"),
    };
    let unquote_edit = Edit::range_replacement(new_content, range);

    let fix = if add_future_import {
        let import_edit = checker.importer().add_future_import();
        Fix::unsafe_edits(unquote_edit, [import_edit])
    } else {
        Fix::safe_edit(unquote_edit)
    };

    checker
        .report_diagnostic(QuotedAnnotation, range)
        .set_fix(fix);
}

fn in_parameter_annotation(offset: TextSize, semantic: &SemanticModel) -> bool {

Review comment (suggestion):
Minor simplification:

```suggestion
        .rfind(|tok| !tok.kind().is_any_newline())
```

Code after:
    fn message(&self) -> String {
        "Remove quotes from type annotation".to_string()
    }

    fn fix_title(&self) -> String {
        "Remove quotes".to_string()
    }
}

/// UP037
pub(crate) fn quoted_annotation(checker: &Checker, annotation: &str, range: TextRange) {
    let add_future_import = checker.settings().future_annotations
        && checker.semantic().in_runtime_evaluated_annotation();

    if !(checker.semantic().in_typing_only_annotation() || add_future_import) {
        return;
    }

    let placeholder_range = TextRange::up_to(annotation.text_len());
    let spans_multiple_lines = annotation.contains_line_break(placeholder_range);

    let last_token_is_comment = checker
        .tokens()
        .iter()
        .rfind(|tok| !tok.kind().is_any_newline())
        .is_some_and(|tok| tok.kind() == TokenKind::Comment);

    let new_content = match (spans_multiple_lines, last_token_is_comment) {
        (_, false) if in_parameter_annotation(range.start(), checker.semantic()) => {
            annotation.to_string()
        }
        (false, false) => annotation.to_string(),
        (true, false) => format!("({annotation})"),
        (_, true) => format!("({annotation}\n)"),
    };
    let unquote_edit = Edit::range_replacement(new_content, range);

    let fix = if add_future_import {
        let import_edit = checker.importer().add_future_import();
        Fix::unsafe_edits(unquote_edit, [import_edit])
    } else {
        Fix::safe_edit(unquote_edit)
    };

    checker
        .report_diagnostic(QuotedAnnotation, range)
        .set_fix(fix);
}

fn in_parameter_annotation(offset: TextSize, semantic: &SemanticModel) -> bool {
    let Stmt::FunctionDef(stmt) = semantic.current_statement() else {