mirror of https://github.com/astral-sh/ruff
Remove pylint's duplicate_value.rs (#6604)
This was moved to bugbear, but we forgot to delete the file.
This commit is contained in:
parent
097db2fcce
commit
3f1658a25b
|
|
@ -1,57 +0,0 @@
|
||||||
use ruff_python_ast::{self as ast, Expr, Ranged};
|
|
||||||
use rustc_hash::FxHashSet;
|
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
|
||||||
use ruff_python_ast::comparable::ComparableExpr;
|
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
|
||||||
|
|
||||||
/// ## What it does
|
|
||||||
/// Checks for set literals that contain duplicate values.
|
|
||||||
///
|
|
||||||
/// ## Why is this bad?
|
|
||||||
/// In Python, sets are unordered collections of unique elements. Including a
|
|
||||||
/// duplicate value in a set literal is redundant, and may be indicative of a
|
|
||||||
/// mistake.
|
|
||||||
///
|
|
||||||
/// ## Example
|
|
||||||
/// ```python
|
|
||||||
/// {1, 2, 3, 1}
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// Use instead:
|
|
||||||
/// ```python
|
|
||||||
/// {1, 2, 3}
|
|
||||||
/// ```
|
|
||||||
#[violation]
|
|
||||||
pub struct DuplicateValue {
|
|
||||||
value: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Violation for DuplicateValue {
|
|
||||||
#[derive_message_formats]
|
|
||||||
fn message(&self) -> String {
|
|
||||||
let DuplicateValue { value } = self;
|
|
||||||
format!("Duplicate value `{value}` in set")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// PLW0130
|
|
||||||
pub(crate) fn duplicate_value(checker: &mut Checker, elts: &Vec<Expr>) {
|
|
||||||
let mut seen_values: FxHashSet<ComparableExpr> = FxHashSet::default();
|
|
||||||
for elt in elts {
|
|
||||||
if let Expr::Constant(ast::ExprConstant { value, .. }) = elt {
|
|
||||||
let comparable_value: ComparableExpr = elt.into();
|
|
||||||
|
|
||||||
if !seen_values.insert(comparable_value) {
|
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
|
||||||
DuplicateValue {
|
|
||||||
value: checker.generator().constant(value),
|
|
||||||
},
|
|
||||||
elt.range(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue