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

Code before:
        }
    }

    fn fix_title(&self) -> Option<String> {
        Some("Remove duplicate item".to_string())
    }
}

/// B033
pub(crate) fn duplicate_value(checker: &Checker, set: &ast::ExprSet) {
    let mut seen_values: FxHashMap<HashableExpr, &Expr> = FxHashMap::default();
    for (index, value) in set.iter().enumerate() {
        if value.is_literal_expr() {
            if let Some(existing) = seen_values.insert(HashableExpr::from(value), value) {
                let mut diagnostic = checker.report_diagnostic(
                    DuplicateValue {
                        value: checker.generator().expr(value),
                        existing: checker.generator().expr(existing),
                    },
                    value.range(),
                );

                diagnostic.secondary_annotation(format_args!("previous occurrence here"), existing);

                diagnostic.set_primary_message(format_args!("duplicated here"));

                diagnostic.try_set_fix(|| {
                    edits::remove_member(&set.elts, index, checker.locator().contents())
                        .map(Fix::safe_edit)
                });
            }
        }
    }
}


Review comment (refactor):
```suggestion
                diagnostic.secondary_annotation(format_args!("previous occurrence here"), existing);
                diagnostic.set_primary_message(format_args!("duplicated here"));
```

Code after:
}

impl Violation for DuplicateValue {
    const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes;

    #[derive_message_formats]
    fn message(&self) -> String {
        let DuplicateValue { value, existing } = self;
        if value == existing {
            format!("Sets should not contain duplicate item `{value}`")
        } else {
            format!(
                "Sets should not contain duplicate items, but `{existing}` and `{value}` has the same value"
            )
        }
    }

    fn fix_title(&self) -> Option<String> {
        Some("Remove duplicate item".to_string())
    }
}

/// B033
pub(crate) fn duplicate_value(checker: &Checker, set: &ast::ExprSet) {
    let mut seen_values: FxHashMap<HashableExpr, &Expr> = FxHashMap::default();
    for (index, value) in set.iter().enumerate() {
        if value.is_literal_expr() {
            if let Some(existing) = seen_values.insert(HashableExpr::from(value), value) {
                let mut diagnostic = checker.report_diagnostic(
                    DuplicateValue {
                        value: checker.generator().expr(value),
                        existing: checker.generator().expr(existing),
                    },
                    value.range(),
                );
                diagnostic.secondary_annotation("Previous occurrence here", existing);
                diagnostic.try_set_fix(|| {
                    edits::remove_member(&set.elts, index, checker.locator().contents()).map(
                        |edit| {
                            let applicability = if checker.comment_ranges().intersects(edit.range())
                            {
                                Applicability::Unsafe
                            } else {
                                Applicability::Safe
                            };
                            Fix::applicable_edit(edit, applicability)
                        },
                    )
                });
            }
        }