mirror of
				https://github.com/astral-sh/ruff.git
				synced 2025-11-03 21:24:29 +00:00 
			
		
		
		
	Use then-some pattern for sometimes-fixable rules (#3199)
This commit is contained in:
		
							parent
							
								
									f38624824d
								
							
						
					
					
						commit
						0f37a98d91
					
				
					 7 changed files with 25 additions and 61 deletions
				
			
		| 
						 | 
				
			
			@ -66,12 +66,8 @@ impl Violation for CompositeAssertion {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
 | 
			
		||||
        let CompositeAssertion { fixable } = self;
 | 
			
		||||
        if *fixable {
 | 
			
		||||
            Some(|_| format!("Break down assertion into multiple parts"))
 | 
			
		||||
        } else {
 | 
			
		||||
            None
 | 
			
		||||
        }
 | 
			
		||||
        self.fixable
 | 
			
		||||
            .then_some(|_| format!("Break down assertion into multiple parts"))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -48,12 +48,8 @@ impl Violation for CollapsibleIf {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
 | 
			
		||||
        let CollapsibleIf { fixable, .. } = self;
 | 
			
		||||
        if *fixable {
 | 
			
		||||
            Some(|_| format!("Combine `if` statements using `and`"))
 | 
			
		||||
        } else {
 | 
			
		||||
            None
 | 
			
		||||
        }
 | 
			
		||||
        self.fixable
 | 
			
		||||
            .then_some(|_| format!("Combine `if` statements using `and`"))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -73,12 +69,9 @@ impl Violation for NeedlessBool {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
 | 
			
		||||
        let NeedlessBool { fixable, .. } = self;
 | 
			
		||||
        if *fixable {
 | 
			
		||||
            Some(|NeedlessBool { condition, .. }| format!("Replace with `return {condition}`"))
 | 
			
		||||
        } else {
 | 
			
		||||
            None
 | 
			
		||||
        }
 | 
			
		||||
        self.fixable.then_some(|NeedlessBool { condition, .. }| {
 | 
			
		||||
            format!("Replace with `return {condition}`")
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -124,18 +117,14 @@ impl Violation for UseTernaryOperator {
 | 
			
		|||
    #[derive_message_formats]
 | 
			
		||||
    fn message(&self) -> String {
 | 
			
		||||
        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> {
 | 
			
		||||
        let UseTernaryOperator { fixable, .. } = self;
 | 
			
		||||
        if *fixable {
 | 
			
		||||
            Some(|UseTernaryOperator { contents, .. }| {
 | 
			
		||||
                format!("Replace if-else-block with `{contents}`")
 | 
			
		||||
        self.fixable
 | 
			
		||||
            .then_some(|UseTernaryOperator { 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> {
 | 
			
		||||
        let DictGetWithDefault { fixable, .. } = self;
 | 
			
		||||
        if *fixable {
 | 
			
		||||
            Some(|DictGetWithDefault { contents, .. }| format!("Replace with `{contents}`"))
 | 
			
		||||
        } else {
 | 
			
		||||
            None
 | 
			
		||||
        }
 | 
			
		||||
        self.fixable
 | 
			
		||||
            .then_some(|DictGetWithDefault { contents, .. }| format!("Replace with `{contents}`"))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -55,12 +55,8 @@ impl Violation for MultipleWithStatements {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
 | 
			
		||||
        let MultipleWithStatements { fixable, .. } = self;
 | 
			
		||||
        if *fixable {
 | 
			
		||||
            Some(|_| format!("Combine `with` statements"))
 | 
			
		||||
        } else {
 | 
			
		||||
            None
 | 
			
		||||
        }
 | 
			
		||||
        self.fixable
 | 
			
		||||
            .then_some(|_| format!("Combine `with` statements"))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,7 +26,7 @@ impl Violation for LambdaAssignment {
 | 
			
		|||
 | 
			
		||||
    fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
 | 
			
		||||
        self.fixable
 | 
			
		||||
            .then_some(|v| format!("Rewrite `{}` as a `def`", v.name))
 | 
			
		||||
            .then_some(|LambdaAssignment { name, .. }| format!("Rewrite `{name}` as a `def`"))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -25,14 +25,10 @@ impl Violation for ConsiderUsingFromImport {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
 | 
			
		||||
        let ConsiderUsingFromImport { fixable, .. } = self;
 | 
			
		||||
        if *fixable {
 | 
			
		||||
            Some(|ConsiderUsingFromImport { module, name, .. }| {
 | 
			
		||||
        self.fixable
 | 
			
		||||
            .then_some(|ConsiderUsingFromImport { module, name, .. }| {
 | 
			
		||||
                format!("Replace with `from {module} import {name}`")
 | 
			
		||||
            })
 | 
			
		||||
        } else {
 | 
			
		||||
            None
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -32,12 +32,8 @@ impl Violation for ImportReplacements {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
 | 
			
		||||
        let ImportReplacements { fixable, .. } = self;
 | 
			
		||||
        if *fixable {
 | 
			
		||||
            Some(|ImportReplacements { module, .. }| format!("Import from `{module}`"))
 | 
			
		||||
        } else {
 | 
			
		||||
            None
 | 
			
		||||
        }
 | 
			
		||||
        self.fixable
 | 
			
		||||
            .then_some(|ImportReplacements { module, .. }| format!("Import from `{module}`"))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -24,16 +24,11 @@ impl Violation for UnpackInsteadOfConcatenatingToCollectionLiteral {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
 | 
			
		||||
        let UnpackInsteadOfConcatenatingToCollectionLiteral { fixable, .. } = self;
 | 
			
		||||
        if *fixable {
 | 
			
		||||
            Some(
 | 
			
		||||
                |UnpackInsteadOfConcatenatingToCollectionLiteral { expr, .. }| {
 | 
			
		||||
                    format!("Replace with `{expr}`")
 | 
			
		||||
                },
 | 
			
		||||
            )
 | 
			
		||||
        } else {
 | 
			
		||||
            None
 | 
			
		||||
        }
 | 
			
		||||
        self.fixable.then_some(
 | 
			
		||||
            |UnpackInsteadOfConcatenatingToCollectionLiteral { expr, .. }| {
 | 
			
		||||
                format!("Replace with `{expr}`")
 | 
			
		||||
            },
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue