Use then-some pattern for sometimes-fixable rules (#3199)

This commit is contained in:
Charlie Marsh 2023-02-23 22:57:14 -05:00 committed by GitHub
parent f38624824d
commit 0f37a98d91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 61 deletions

View file

@ -66,12 +66,8 @@ impl Violation for CompositeAssertion {
} }
fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> { fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
let CompositeAssertion { fixable } = self; self.fixable
if *fixable { .then_some(|_| format!("Break down assertion into multiple parts"))
Some(|_| format!("Break down assertion into multiple parts"))
} else {
None
}
} }
} }

View file

@ -48,12 +48,8 @@ impl Violation for CollapsibleIf {
} }
fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> { fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
let CollapsibleIf { fixable, .. } = self; self.fixable
if *fixable { .then_some(|_| format!("Combine `if` statements using `and`"))
Some(|_| format!("Combine `if` statements using `and`"))
} else {
None
}
} }
} }
@ -73,12 +69,9 @@ impl Violation for NeedlessBool {
} }
fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> { fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
let NeedlessBool { fixable, .. } = self; self.fixable.then_some(|NeedlessBool { condition, .. }| {
if *fixable { format!("Replace with `return {condition}`")
Some(|NeedlessBool { condition, .. }| format!("Replace with `return {condition}`")) })
} else {
None
}
} }
} }
@ -124,18 +117,14 @@ impl Violation for UseTernaryOperator {
#[derive_message_formats] #[derive_message_formats]
fn message(&self) -> String { fn message(&self) -> String {
let UseTernaryOperator { contents, .. } = self; let UseTernaryOperator { contents, .. } = self;
format!("Use ternary operator `{contents}` instead of if-else-block") format!("Use ternary operator `{contents}` instead of `if`-`else`-block")
} }
fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> { fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
let UseTernaryOperator { fixable, .. } = self; self.fixable
if *fixable { .then_some(|UseTernaryOperator { contents, .. }| {
Some(|UseTernaryOperator { contents, .. }| { format!("Replace `if`-`else`-block with `{contents}`")
format!("Replace if-else-block with `{contents}`")
}) })
} else {
None
}
} }
} }
@ -185,12 +174,8 @@ impl Violation for DictGetWithDefault {
} }
fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> { fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
let DictGetWithDefault { fixable, .. } = self; self.fixable
if *fixable { .then_some(|DictGetWithDefault { contents, .. }| format!("Replace with `{contents}`"))
Some(|DictGetWithDefault { contents, .. }| format!("Replace with `{contents}`"))
} else {
None
}
} }
} }

View file

@ -55,12 +55,8 @@ impl Violation for MultipleWithStatements {
} }
fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> { fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
let MultipleWithStatements { fixable, .. } = self; self.fixable
if *fixable { .then_some(|_| format!("Combine `with` statements"))
Some(|_| format!("Combine `with` statements"))
} else {
None
}
} }
} }

View file

@ -26,7 +26,7 @@ impl Violation for LambdaAssignment {
fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> { fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
self.fixable self.fixable
.then_some(|v| format!("Rewrite `{}` as a `def`", v.name)) .then_some(|LambdaAssignment { name, .. }| format!("Rewrite `{name}` as a `def`"))
} }
} }

View file

@ -25,14 +25,10 @@ impl Violation for ConsiderUsingFromImport {
} }
fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> { fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
let ConsiderUsingFromImport { fixable, .. } = self; self.fixable
if *fixable { .then_some(|ConsiderUsingFromImport { module, name, .. }| {
Some(|ConsiderUsingFromImport { module, name, .. }| {
format!("Replace with `from {module} import {name}`") format!("Replace with `from {module} import {name}`")
}) })
} else {
None
}
} }
} }

View file

@ -32,12 +32,8 @@ impl Violation for ImportReplacements {
} }
fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> { fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
let ImportReplacements { fixable, .. } = self; self.fixable
if *fixable { .then_some(|ImportReplacements { module, .. }| format!("Import from `{module}`"))
Some(|ImportReplacements { module, .. }| format!("Import from `{module}`"))
} else {
None
}
} }
} }

View file

@ -24,16 +24,11 @@ impl Violation for UnpackInsteadOfConcatenatingToCollectionLiteral {
} }
fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> { fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
let UnpackInsteadOfConcatenatingToCollectionLiteral { fixable, .. } = self; self.fixable.then_some(
if *fixable {
Some(
|UnpackInsteadOfConcatenatingToCollectionLiteral { expr, .. }| { |UnpackInsteadOfConcatenatingToCollectionLiteral { expr, .. }| {
format!("Replace with `{expr}`") format!("Replace with `{expr}`")
}, },
) )
} else {
None
}
} }
} }