Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hash_value_overflow rule #2496

Merged
merged 14 commits into from
Dec 5, 2018
Merged

Added hash_value_overflow rule #2496

merged 14 commits into from
Dec 5, 2018

Conversation

kimdv
Copy link
Contributor

@kimdv kimdv commented Dec 3, 2018

This fixes #2108

I'm not good at all to reggex. So I got a a little help from a friend 😅

@jpsim
Copy link
Collaborator

jpsim commented Dec 3, 2018

Thanks for the PR @kimdv! Do you think we should be encouraging the use of Swift 4.2's modernized hashing instead?

This rule seems especially well suited to avoid using a regular expression to detect the use of +/* and instead using string matching.

For context, the heavy use of regular expressions in SwiftLint have been contributing to performance cliffs and false positives and I would like us to start weaning off of them.

@SwiftLintBot
Copy link

SwiftLintBot commented Dec 3, 2018

59 Warnings
⚠️ This PR introduced a violation in Firefox: /Users/distiller/project/osscheck/Firefox/Account/KeyBundle.swift:141:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Firefox: /Users/distiller/project/osscheck/Firefox/Storage/Visit.swift:63:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Firefox: /Users/distiller/project/osscheck/Firefox/Storage/Visit.swift:97:19: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Firefox: /Users/distiller/project/osscheck/Firefox/Sync/State.swift:99:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Kickstarter: /Users/distiller/project/osscheck/Kickstarter/KsApi/GraphSchema.swift:21:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Kickstarter: /Users/distiller/project/osscheck/Kickstarter/KsApi/models/Category.swift:101:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Kickstarter: /Users/distiller/project/osscheck/Kickstarter/KsApi/models/Category.swift:140:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Kickstarter: /Users/distiller/project/osscheck/Kickstarter/KsApi/models/DiscoveryParams.swift:77:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Kickstarter: /Users/distiller/project/osscheck/Kickstarter/Library/Format.swift:326:15: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Kickstarter: /Users/distiller/project/osscheck/Kickstarter/Library/Format.swift:395:15: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Kickstarter: /Users/distiller/project/osscheck/Kickstarter/Library/RefTag.swift:219:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Moya: /Users/distiller/project/osscheck/Moya/Sources/Moya/Endpoint.swift:122:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in SourceKitten: /Users/distiller/project/osscheck/SourceKitten/Source/SourceKittenFramework/SourceDeclaration.swift:181:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in SourceKitten: /Users/distiller/project/osscheck/SourceKitten/Source/SourceKittenFramework/UID.swift:46:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/private/StdlibUnittest/LifetimeTracked.swift:51:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/private/StdlibUnittest/MinimalTypes.swift:142:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/private/StdlibUnittest/MinimalTypes.swift:195:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/private/StdlibUnittest/MinimalTypes.swift:253:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/private/StdlibUnittest/MinimalTypes.swift:309:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/private/StdlibUnittest/StdlibCoreExtras.swift:119:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/private/StdlibUnittest/StringConvertible.swift:64:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/core/AnyHashable.swift:238:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/core/CompilerProtocols.swift:187:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/core/Hashable.swift:109:3: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/core/FloatingPoint.swift:1242:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/core/NewtypeWrapper.swift:22:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/core/KeyPath.swift:46:16: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/core/UnicodeScalarProperties.swift:1275:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/CoreFoundation/CoreFoundation.swift:17:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/AffineTransform.swift:279:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/Date.swift:145:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/CharacterSet.swift:52:17: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/CharacterSet.swift:754:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/DateComponents.swift:266:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/DateInterval.swift:158:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/Decimal.swift:180:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/Calendar.swift:902:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/IndexPath.swift:665:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/IndexSet.swift:140:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/Locale.swift:408:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/Notification.swift:42:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/Measurement.swift:39:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/Data.swift:1647:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/NSRange.swift:16:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/NSStringEncodings.swift:54:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/PersonNameComponents.swift:74:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/TimeZone.swift:199:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/URLComponents.swift:303:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/URLComponents.swift:415:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/NSError.swift:409:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/NSError.swift:484:3: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/NSError.swift:572:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/NSError.swift:1803:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/UUID.swift:77:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/URLRequest.swift:232:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/URL.swift:635:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/ObjectiveC/ObjectiveC.swift:219:10: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in WordPress: /Users/distiller/project/osscheck/WordPress/WordPress/Classes/Models/JetpackSiteRef.swift:26:5: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
⚠️ This PR introduced a violation in WordPress: /Users/distiller/project/osscheck/WordPress/WordPressFlux/WordPressFlux/Sources/Dispatcher.swift:6:12: warning: Legacy Hashing Violation: Prefer using the hash(into:) function instead of overriding hashValue (legacy_hashing)
12 Messages
📖 Linting Aerial with this PR took 2.07s vs 2.0s on master (3% slower)
📖 Linting Alamofire with this PR took 4.5s vs 4.25s on master (5% slower)
📖 Linting Firefox with this PR took 17.18s vs 15.89s on master (8% slower)
📖 Linting Kickstarter with this PR took 27.25s vs 23.32s on master (16% slower)
📖 Linting Moya with this PR took 2.46s vs 2.28s on master (7% slower)
📖 Linting Nimble with this PR took 2.55s vs 2.12s on master (20% slower)
📖 Linting Quick with this PR took 0.73s vs 0.66s on master (10% slower)
📖 Linting Realm with this PR took 4.71s vs 4.14s on master (13% slower)
📖 Linting SourceKitten with this PR took 1.58s vs 1.33s on master (18% slower)
📖 Linting Sourcery with this PR took 6.21s vs 5.6s on master (10% slower)
📖 Linting Swift with this PR took 38.24s vs 32.97s on master (15% slower)
📖 Linting WordPress with this PR took 28.87s vs 23.8s on master (21% slower)

Generated by 🚫 Danger

@kimdv
Copy link
Contributor Author

kimdv commented Dec 3, 2018

Thanks, @jpsim!

We could do that, but I have just finished a project where we needed custom hashes.

I have seen your comments lately on avoiding regular expression, so I really would like to remove the hash regular expression, so can you point where I could look for an example?

@marcelofabri
Copy link
Collaborator

@kimdv I think @jpsim's suggestion was to use the new func hash(into hasher: inout Hasher) instead of manually combining hashes:

extension GridPoint: Hashable {
  func hash(into hasher: inout Hasher) {
    hasher.combine(x)
    hasher.combine(y)
  }
}

(from https://github.com/apple/swift-evolution/blob/master/proposals/0206-hashable-enhancements.md#the-hashinto-requirement)

@kimdv
Copy link
Contributor Author

kimdv commented Dec 3, 2018

Sure we can do that!
Will make it min requirement Swift 4.2, what do you think?

@marcelofabri can you maybe point out where to look for inspiration to avoid regular expression?

@marcelofabri
Copy link
Collaborator

@kimdv sounds good for min requirement.

If the rule is updated to validate that hash(into:) should be used (instead of checking for overflow values), it becomes easier. You can implement ASTRule and check for hashValue computed properties declarations instead of using regexes. ProhibitedInterfaceBuilderRule seems a good place to start. WeakDelegateRule has a check for computed variables, which will be needed as well.

@kimdv
Copy link
Contributor Author

kimdv commented Dec 3, 2018

Thanks @marcelofabri!

The rule got much simpler 🚀
I think i have got something right here :)

@kimdv
Copy link
Contributor Author

kimdv commented Dec 4, 2018

@marcelofabri it seems that the tests that are failing is Swiftlint itself.

Should i fix those places in Swiftlint where it violates the rule?

@marcelofabri
Copy link
Collaborator

@kimdv Please do it!

@kimdv
Copy link
Contributor Author

kimdv commented Dec 4, 2018

Pushed changes and stuff starts to pass now! 🚀

@marcelofabri
Copy link
Collaborator

@kimdv Thanks for this - just had some minor comments but overall it looks great!

We also need a changelog entry for this change ✨

@kimdv
Copy link
Contributor Author

kimdv commented Dec 5, 2018

@marcelofabri I rebased with master (Don't know if I did it right) and added your feedback and added changelog entry

CHANGELOG.md Outdated Show resolved Hide resolved
@kimdv
Copy link
Contributor Author

kimdv commented Dec 5, 2018

@jpsim should be resolved now 🚀

@jpsim
Copy link
Collaborator

jpsim commented Dec 5, 2018

Should we name this rule "Legacy Hashing" to align with the other legacy family of rules?

  • Legacy CGGeometry Functions
  • Legacy Constant
  • Legacy Constructor
  • Legacy NSGeometry Functions
  • Legacy Random

CHANGELOG.md Outdated Show resolved Hide resolved
@jpsim jpsim merged commit d62e187 into realm:master Dec 5, 2018
@jpsim
Copy link
Collaborator

jpsim commented Dec 5, 2018

Merged! 🎉

Thanks @kimdv !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule Request: [HashValue overflow]
4 participants