@@ -25,20 +25,17 @@ fileprivate let knownIntTypes = Set(intSizes.map { "Int\($0)" } + intSizes.map {
2525/// Lint: If an initializer-style cast is used on a built-in type known to be expressible by
2626/// that kind of literal type, a lint error is raised.
2727///
28- /// Format: Initializer-style casts between known built-in types will be converted to standard
29- /// casts.
30- ///
3128/// - SeeAlso: https://google.github.io/swift#numeric-and-string-literals
32- public final class AvoidInitializersForLiterals : SyntaxFormatRule {
33- public override func visit( _ node: FunctionCallExprSyntax ) -> ExprSyntax {
29+ public final class AvoidInitializersForLiterals : SyntaxLintRule {
30+ public override func visit( _ node: FunctionCallExprSyntax ) {
3431 // Ensure we're calling a known Integer initializer.
3532 guard let callee = node. calledExpression as? IdentifierExprSyntax else {
3633 // Ensure we properly visit the children of this node, in case we have other function calls
3734 // as parameters to this one.
3835 return super. visit ( node)
3936 }
4037
41- guard node. argumentList. count < = 1 else {
38+ guard node. argumentList. count = = 1 else {
4239 return super. visit ( node)
4340 }
4441
@@ -50,44 +47,13 @@ public final class AvoidInitializersForLiterals: SyntaxFormatRule {
5047
5148 let typeName = callee. identifier. text
5249
53- guard let literal = extractLiteral ( node, typeName) else {
50+ guard let _ = extractLiteral ( node, typeName) else {
5451 return super. visit ( node)
5552 }
5653
57- diagnose ( . avoidInitializerStyleCast, on: callee) {
54+ diagnose ( . avoidInitializerStyleCast( node . description ) , on: callee) {
5855 $0. highlight ( callee. sourceRange ( in: self . context. fileURL) )
5956 }
60-
61- // Construct an 'as' cast, converting `X(y)` to `y as X`.
62- let asExpr = AsExprSyntax {
63- $0. useAsTok ( SyntaxFactory . makeAsKeyword (
64- trailingTrivia: . spaces( 1 )
65- ) )
66- $0. useTypeName (
67- SyntaxFactory . makeSimpleTypeIdentifier (
68- name: callee. identifier,
69- genericArgumentClause: nil
70- )
71- )
72- }
73-
74- let newAs = replaceTrivia (
75- on: asExpr,
76- token: asExpr. lastToken,
77- trailingTrivia: node. trailingTrivia
78- ) as! AsExprSyntax
79-
80- let newLiteral = replaceTrivia (
81- on: literal,
82- token: literal. firstToken,
83- trailingTrivia: . spaces( 1 )
84- ) as! ExprSyntax
85-
86- return SyntaxFactory . makeSequenceExpr (
87- elements: SyntaxFactory . makeExprList ( [
88- newLiteral,
89- newAs
90- ] ) )
9157 }
9258}
9359
@@ -105,6 +71,7 @@ fileprivate func extractLiteral(_ node: FunctionCallExprSyntax, _ typeName: Stri
10571}
10672
10773extension Diagnostic . Message {
108- static let avoidInitializerStyleCast =
109- Diagnostic . Message ( . warning, " change initializer call with literal argument to an 'as' cast " )
74+ static func avoidInitializerStyleCast( _ name: String ) -> Diagnostic . Message {
75+ return . init( . warning, " change initializer call ' \( name) ' with literal argument to an 'as' cast " )
76+ }
11077}
0 commit comments