Silence `xct_specific_matcher` rule on types and tuples used in "one argument asserts" (#4997)

This commit is contained in:
Danny Mösch 2023-05-13 22:04:35 +02:00 committed by GitHub
parent 1e73835e83
commit 4d4a330d46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 4 deletions

View File

@ -10,7 +10,11 @@
#### Enhancements
* None.
* Silence `xct_specific_matcher` rule on "one argument asserts" if there are
potential types or tuples involved in the comparison as types and tuples do
not conform to `Equatable`.
[SimplyDanny](https://github.com/SimplyDanny)
[#4990](https://github.com/realm/SwiftLint/issues/4990)
#### Bug Fixes

View File

@ -71,12 +71,26 @@ private enum OneArgXCTAssert: String {
let matcher = Self(rawValue: name),
let argument = node.argumentList.first?.expression.as(SequenceExprSyntax.self),
let folded = try? OperatorTable.standardOperators.foldSingle(argument),
let binOp = folded.as(InfixOperatorExprSyntax.self)?.operatorOperand.as(BinaryOperatorExprSyntax.self),
let kind = Comparison(rawValue: binOp.operatorToken.text) else {
let operatorExpr = folded.as(InfixOperatorExprSyntax.self),
let binOp = operatorExpr.operatorOperand.as(BinaryOperatorExprSyntax.self),
let kind = Comparison(rawValue: binOp.operatorToken.text),
accept(operand: operatorExpr.leftOperand), accept(operand: operatorExpr.rightOperand) else {
return nil
}
return matcher.suggestion(for: kind)
}
private static func accept(operand: ExprSyntax) -> Bool {
// Check if the expression could be a type object like `String.self`. Note, however, that `1.self`
// is also valid Swift. There is no way to be sure here.
if operand.as(MemberAccessExprSyntax.self)?.name.text == "self" {
return false
}
if operand.as(TupleExprSyntax.self)?.elementList.count ?? 0 > 1 {
return false
}
return true
}
}
private enum TwoArgsXCTAssert: String {

View File

@ -58,7 +58,12 @@ internal struct XCTSpecificMatcherRuleExamples {
excludeFromDocumentation: true),
Example("XCTAssert(foo == bar)",
configuration: ["matchers": ["two-argument-asserts"]],
excludeFromDocumentation: true)
excludeFromDocumentation: true),
// Skip if one operand might be a type or a tuple
Example("XCTAssert(foo.self == bar)"),
Example("XCTAssertTrue(type(of: foo) != Int.self)"),
Example("XCTAssertTrue(a == (1, 3, 5)")
]
static let triggeringExamples = [