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

Untyped error checks in catch statement #2055

Closed
wants to merge 1 commit into from

Conversation

dirtydanee
Copy link
Contributor

Implements #2045

@ornithocoder
Copy link
Contributor

Don't forget to add your entry to the change log :-)

@SwiftLintBot
Copy link

SwiftLintBot commented Feb 17, 2018

38 Warnings
⚠️ This PR introduced a violation in Aerial: /Users/distiller/project/osscheck/Aerial/Aerial/Source/Models/Cache/VideoCache.swift:52:19: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Aerial: /Users/distiller/project/osscheck/Aerial/Aerial/Source/Models/Cache/VideoCache.swift:168:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Aerial: /Users/distiller/project/osscheck/Aerial/Aerial/Source/Models/Cache/VideoDownload.swift:169:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Firefox: /Users/distiller/project/osscheck/Firefox/Client/Application/AdjustIntegration.swift:150:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Firefox: /Users/distiller/project/osscheck/Firefox/Client/Application/AdjustIntegration.swift:166:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Firefox: /Users/distiller/project/osscheck/Firefox/Providers/Profile.swift:1070:15: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Firefox: /Users/distiller/project/osscheck/Firefox/Shared/SentryIntegration.swift:77:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Firefox: /Users/distiller/project/osscheck/Firefox/SyncTelemetry/SyncPingCentre.swift:98:19: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Firefox: /Users/distiller/project/osscheck/Firefox/SyncTelemetry/SyncPingCentre.swift:116:19: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Firefox: /Users/distiller/project/osscheck/Firefox/SyncTelemetry/SyncTelemetryEvents.swift:66:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Firefox: /Users/distiller/project/osscheck/Firefox/SyncTelemetry/SyncTelemetryEvents.swift:83:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Firefox: /Users/distiller/project/osscheck/Firefox/Sync/Synchronizers/Bookmarks/ThreeWayTreeMerger.swift:1053:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Kickstarter: /Users/distiller/project/osscheck/Kickstarter/KsApi/Service.swift:524:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Moya: /Users/distiller/project/osscheck/Moya/Sources/Moya/Response.swift:168:19: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Moya: /Users/distiller/project/osscheck/Moya/Sources/Moya/Response.swift:184:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Nimble: /Users/distiller/project/osscheck/Nimble/Sources/Nimble/Adapters/NMBObjCMatcher.swift:51:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Nimble: /Users/distiller/project/osscheck/Nimble/Sources/Nimble/DSL+Wait.swift:62:27: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Nimble: /Users/distiller/project/osscheck/Nimble/Sources/Nimble/Expectation.swift:15:7: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Nimble: /Users/distiller/project/osscheck/Nimble/Sources/Nimble/Expectation.swift:33:7: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Nimble: /Users/distiller/project/osscheck/Nimble/Sources/Nimble/Expectation.swift:51:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Nimble: /Users/distiller/project/osscheck/Nimble/Sources/Nimble/Matchers/ThrowAssertion.swift:30:15: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Nimble: /Users/distiller/project/osscheck/Nimble/Sources/Nimble/Matchers/ThrowError.swift:20:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Nimble: /Users/distiller/project/osscheck/Nimble/Sources/Nimble/Matchers/ThrowError.swift:51:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Nimble: /Users/distiller/project/osscheck/Nimble/Sources/Nimble/Matchers/ThrowError.swift:96:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Nimble: /Users/distiller/project/osscheck/Nimble/Sources/Nimble/Matchers/ThrowError.swift:144:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Nimble: /Users/distiller/project/osscheck/Nimble/Sources/Nimble/Matchers/ThrowError.swift:203:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Nimble: /Users/distiller/project/osscheck/Nimble/Sources/Nimble/Matchers/ThrowError.swift:238:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Nimble: /Users/distiller/project/osscheck/Nimble/Sources/Nimble/Utils/Async.swift:260:15: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Nimble: /Users/distiller/project/osscheck/Nimble/Sources/Nimble/Utils/Async.swift:344:19: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Realm: /Users/distiller/project/osscheck/Realm/RealmSwift/Realm.swift:156:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Dispatch/Queue.swift:239:7: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Dispatch/Queue.swift:263:6: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/IndexSet.swift:591:19: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in Swift: /Users/distiller/project/osscheck/Swift/stdlib/public/SDK/Foundation/PlistEncoder.swift:495:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in WordPress: /Users/distiller/project/osscheck/WordPress/WordPress/Classes/Services/InAppPurchaseStore.swift:241:19: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in WordPress: /Users/distiller/project/osscheck/WordPress/WordPress/Classes/ViewRelated/Views/WPRichText/WPRichContentView.swift:145:15: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in WordPress: /Users/distiller/project/osscheck/WordPress/WordPress/WordPressScreenshotGeneration/SnapshotHelper.swift:76:11: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
⚠️ This PR introduced a violation in WordPress: /Users/distiller/project/osscheck/WordPress/WordPress/WordPressScreenshotGeneration/SnapshotHelper.swift:142:15: warning: Untyped error in catch rule Violation: warning (untyped_error_in_catch)
12 Messages
📖 Linting Aerial with this PR took 0.42s vs 0.43s on master (2% faster)
📖 Linting Alamofire with this PR took 3.45s vs 3.59s on master (3% faster)
📖 Linting Firefox with this PR took 13.67s vs 13.85s on master (1% faster)
📖 Linting Kickstarter with this PR took 21.73s vs 21.68s on master (0% slower)
📖 Linting Moya with this PR took 2.02s vs 2.07s on master (2% faster)
📖 Linting Nimble with this PR took 1.98s vs 2.01s on master (1% faster)
📖 Linting Quick with this PR took 0.59s vs 0.59s on master (0% slower)
📖 Linting Realm with this PR took 3.74s vs 3.83s on master (2% faster)
📖 Linting SourceKitten with this PR took 1.19s vs 1.2s on master (0% faster)
📖 Linting Sourcery with this PR took 5.1s vs 5.6s on master (8% faster)
📖 Linting Swift with this PR took 14.64s vs 16.7s on master (12% faster)
📖 Linting WordPress with this PR took 16.88s vs 18.03s on master (6% faster)

Generated by 🚫 Danger

@codecov-io
Copy link

codecov-io commented Feb 17, 2018

Codecov Report

Merging #2055 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2055      +/-   ##
==========================================
+ Coverage   89.68%   89.71%   +0.02%     
==========================================
  Files         259      260       +1     
  Lines       15020    15039      +19     
  Branches      977      978       +1     
==========================================
+ Hits        13471    13492      +21     
+ Misses       1532     1530       -2     
  Partials       17       17
Impacted Files Coverage Δ
...tLintFramework/Rules/UntypedErrorInCatchRule.swift 100% <100%> (ø)
Tests/SwiftLintFrameworkTests/RulesTests.swift 100% <100%> (ø) ⬆️
Source/SwiftLintFramework/Models/Command.swift 97.82% <0%> (+2.17%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 52274de...c67937b. Read the comment docs.

import Foundation
import SourceKittenFramework

public struct UntypedErrorRule: ASTRule, OptInRule, ConfigurationProviderRule {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about changing it to UntypedErrorInCatchRule

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then chaning the rule identifier and name as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the naming as you suggested, its for sure more clear what does it.

description: "Catch statements should not declare error variables without type casting",
kind: .lint,
nonTriggeringExamples: [
"do {\n try foo() \n} catch {\n}",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you format he entries better? Check how they're rendered in Rules.md. I think just improving the indentation of try foo() would already help.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the indentation and removed the \n from the catch statements scope.

private static let regularExpression = regex(
"catch" + // The catch keyword
"(?:" + // Start of the first non-capturing group
"\\s+" + // At least one any type of whitespace character
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A whitespace is not required if parentheses are used: catch(let error) {.

Also, the rule currently wouldn't catch when they're used even with spaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the pattern to capture the errors with parentheses with and without spaces.

}

return file.lines
.lazy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really improve performance? I think lines will be calculated anyways.

return file.lines
.lazy
.filter { $0.byteRange.contains(offset) }
.reduce([]) { _, line in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be more semantic to use flatMap here?

public static let description = RuleDescription(
identifier: "untyped_error",
name: "Untyped error rule",
description: "Catch statements should not declare error variables without type casting",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a . in the end?

identifier: "untyped_error",
name: "Untyped error rule",
description: "Catch statements should not declare error variables without type casting",
kind: .lint,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you change to .idiomatic? .lint is more for validating things that could indicate a programmer's error.

nonTriggeringExamples: [
"do {\n try foo() \n} catch {\n}",
"do {\n try foo() \n} catch Error.invalidOperation {\n} catch {\n}",
"do {\n try foo() \n} catch let error as? MyError {\n} catch {\n}",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a major issue, but I think catch let error as MyError should be used. I got a warning while compiling with the optional cast:

 warning: cast from '_' to unrelated type 'MyError' always fails

return StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: location,
reason: configuration.consoleDescription)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why you're iterating through lines?

I have the impression that it'd be faster to make this a regular rule (not ASTRule) and just match the regex on the whole file, validating some syntax kinds (e.g. that catch is a keyword, as well as let/var).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcelofabri Thanks for the review! This suggestion was a really spot on. I have decided to use ASTRule to be able to solve the issue with the commented out code. However the syntax kind validation works great, simplified the code by tons and should be faster too.

@dirtydanee dirtydanee force-pushed the untype-error-in-catch branch 2 times, most recently from b2b5b1f to 1fb429d Compare February 19, 2018 12:30
@dirtydanee
Copy link
Contributor Author

@marcelofabri thoughts on the update?

@marcelofabri
Copy link
Collaborator

@dirtydanee Merged in #2101 🎉

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.

5 participants