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

Code before:
            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(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(
                        |edit| {
                            let applicability = if checker.comment_ranges().intersects(edit.range())
                            {
                                Applicability::Unsafe
                            } else {
                                Applicability::Safe
                            };
                            Fix::applicable_edit(edit, applicability)
                        },
                    )
                });
            }
        }
    }
}


Review comment (refactor):
I think this is in line with the two other uses of `set_primary_message` in Ruff, but one thing to consider here is that the primary annotation message is appended to the concise diagnostic message:

```shell
❯ just run check --select B033 --output-format concise - <<<"s = {1, 2, 1}"
-:1:12: B033 [*] Sets should not contain duplicate item `1`: duplicated here
```

I think this looks slightly awkward, so I would lean towards omitting the primary message, even though it looks nicer in the `full` output. But I'm curious what you and @amyreese think!

Code after:
            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)
                        },
                    )
                });
            }
        }
    }
}