SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Add no blanket disables rule

Open mildm8nnered opened this issue 2 years ago • 1 comments

Adds a new SwiftLint rule: no_blanket_disables

This warns (by default) when a SwiftLint rule is disabled, and then never later re-enabled. In many cases this is because the developer forgot to re-enable the rule, or somehow bodged it (e.g. // sswiftlint:enable some_rule), resulting in stretches of code going unintentionally un-linted.

Violations of this rule are surprisingly common. I found a bunch in the SwiftLint source code, which I've patched in various ways in this PR - some could simply be turned into // swiftlint:disable:next some_rule's and some I suppressed.

There are some rules that it doesn't make sense to re-enable, and that should apply to the whole file. An allowed_rules configuration parameter allows these to be specified. By default, file_length and single_test_class are allowed.

For these and possibly other rules, we may want to disable a particular rule always for the entire file, and to warn if it that rule is disabled with a modifier or re-enabled. The always_blanket_disable configuration parameter allows a set of rules to be specified that should always be warned on anything except a blanket disable. By default the set is empty.

This rule will also warn if you use swiftlint:disable to disable a rule that's already disabled, or swiftlint:enable to enable a rule that isn't disabled.

nonTriggeringExamples:

// swiftlint:disable unused_import
// swiftlint:enable unused_import
// swiftlint:disable unused_import unused_declaration
// swiftlint:enable unused_import
// swiftlint:enable unused_declaration
// swiftlint:disable:this unused_import")
// swiftlint:disable:next unused_import")
// swiftlint:disable:previous unused_import")

triggeringExamples:

// swiftlint:disable unused_import
// swiftlint:disable unused_import unused_declaration
// swiftlint:enable unused_import
// swiftlint:disable unused_import
// swiftlint:disable unused_import
// swiftlint:enable unused_import
// swiftlint:enable unused_import

See https://github.com/realm/SwiftLint/issues/4684

mildm8nnered avatar Jan 29 '23 18:01 mildm8nnered

70 Warnings
:warning: This PR introduced a violation in Aerial: /Aerial/Source/Models/AerialVideo.swift:76:26: warning: Blanket Disable Command Violation: The disabled 'cyclomatic_complexity' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Aerial: /Aerial/Source/Models/Cache/Cache.swift:437:42: warning: Blanket Disable Command Violation: The disabled 'for_where' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Aerial: /Aerial/Source/Models/Cache/Cache.swift:437:42: warning: Blanket Disable Command Violation: The disabled 'for_where' rule was already disabled (blanket_disable_command)
:warning: This PR introduced a violation in Aerial: /Aerial/Source/Models/Cache/Cache.swift:597:26: warning: Blanket Disable Command Violation: The disabled 'line_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Aerial: /Aerial/Source/Models/Sources/SourceList.swift:101:26: warning: Blanket Disable Command Violation: The disabled 'for_where' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Aerial: /Aerial/Source/Views/Sources/CheckboxCellView.swift:10:22: warning: Blanket Disable Command Violation: The disabled 'class_delegate_protocol' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Aerial: /Aerial/Source/Views/Sources/CheckboxCellView.swift:10:46: warning: Blanket Disable Command Violation: The disabled 'weak_delegate' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Aerial: /Resources/MainUI/Settings panels/SourcesViewController.swift:222:26: warning: Blanket Disable Command Violation: The disabled 'cyclomatic_complexity' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Aerial: /Resources/MainUI/Settings panels/TimeViewController.swift:55:26: warning: Blanket Disable Command Violation: The disabled 'cyclomatic_complexity' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Aerial: /Resources/MainUI/SidebarViewController.swift:430:1: warning: Blanket Disable Command Violation: The disabled '}' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Aerial: /Resources/MainUI/SidebarViewController.swift:430:1: warning: Blanket Disable Command Violation: The disabled '}*/' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Brave: /Sources/BraveShared/BraveStrings.swift:19:22: warning: Blanket Disable Command Violation: The disabled 'line_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in DuckDuckGo: /Core/APIRequest.swift:26:22: warning: Blanket Disable Command Violation: The disabled 'line_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in DuckDuckGo: /Core/Pixel.swift:221:21: warning: Blanket Disable Command Violation: The enabled 'file_length' rule was not disabled (blanket_disable_command)
:warning: This PR introduced a violation in DuckDuckGo: /Core/Pixel.swift:24:21: warning: Blanket Disable Command Violation: The enabled 'type_body_length' rule was not disabled (blanket_disable_command)
:warning: This PR introduced a violation in DuckDuckGo: /Core/PixelEvent.swift:25:22: warning: Blanket Disable Command Violation: The disabled 'type_body_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in DuckDuckGo: /DuckDuckGo/AutofillLoginSettingsListViewController.swift:28:22: warning: Blanket Disable Command Violation: The disabled 'type_body_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in DuckDuckGo: /DuckDuckGo/BrowsingMenu/BrowsingMenuViewController.swift:264:22: warning: Blanket Disable Command Violation: The disabled 'line_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in DuckDuckGo: /DuckDuckGo/DatabaseMigration.swift:77:25: warning: Blanket Disable Command Violation: The enabled 'function_body_length' rule was not disabled (blanket_disable_command)
:warning: This PR introduced a violation in DuckDuckGo: /DuckDuckGo/HomeMessageViewModelBuilder.swift:82:25: warning: Blanket Disable Command Violation: The enabled 'function_body_length' rule was not disabled (blanket_disable_command)
:warning: This PR introduced a violation in DuckDuckGo: /DuckDuckGo/TrackerImageCache.swift:107:26: warning: Blanket Disable Command Violation: The disabled 'cyclomatic_complexity' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in DuckDuckGo: /DuckDuckGo/TrackerImageCache.swift:107:26: warning: Blanket Disable Command Violation: The disabled 'cyclomatic_complexity' rule was already disabled (blanket_disable_command)
:warning: This PR introduced a violation in DuckDuckGo: /DuckDuckGoTests/AutofillLoginListViewModelTests.swift:20:22: warning: Blanket Disable Command Violation: The disabled 'line_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in DuckDuckGo: /DuckDuckGoTests/BookmarksExporterTests.swift:131:25: warning: Blanket Disable Command Violation: The enabled 'line_length' rule was not disabled (blanket_disable_command)
:warning: This PR introduced a violation in DuckDuckGo: /DuckDuckGoTests/BookmarksExporterTests.swift:201:26: warning: Blanket Disable Command Violation: The disabled 'function_body_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Firefox: /Client/Extensions/UIWindow+Extension.swift:7:22: warning: Blanket Disable Command Violation: The disabled 'first_where' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Firefox: /Client/Frontend/Strings.swift:5:22: warning: Blanket Disable Command Violation: The disabled 'line_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Firefox: /Tests/XCUITests/DesktopModeTests.swift:7:22: warning: Blanket Disable Command Violation: The disabled 'empty_count' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Kickstarter: /Library/Strings.swift:7:22: warning: Blanket Disable Command Violation: The disabled 'valid_docs' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Kickstarter: /Library/Strings.swift:8:22: warning: Blanket Disable Command Violation: The disabled 'line_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Kickstarter: /Library/ViewModels/DiscoveryPostcardViewModelTests.swift:7:22: warning: Blanket Disable Command Violation: The disabled 'force_unwrapping' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Kickstarter: /bin/StringsScript/Tests/StringsScriptTests/StringsScriptTests.swift:5:22: warning: Blanket Disable Command Violation: The disabled 'line_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Moya: /Examples/_shared/GitHubAPI.swift:1:22: warning: Blanket Disable Command Violation: The disabled 'force_unwrapping' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Moya: /Tests/MoyaTests/MoyaProviderSpec.swift:1:34: warning: Blanket Disable Command Violation: The disabled 'type_body_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in NetNewsWire: /Widget/Resources/Localized.swift:1:22: warning: Blanket Disable Command Violation: The disabled 'all' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Nimble: /Sources/Nimble/DSL.swift:74:25: warning: Blanket Disable Command Violation: The enabled 'line_length' rule was not disabled (blanket_disable_command)
:warning: This PR introduced a violation in PocketCasts: /podcasts/Strings+Generated.swift:1:22: warning: Blanket Disable Command Violation: The disabled 'all' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in PocketCasts: /podcasts/Strings+L10n.swift:159:22: warning: Blanket Disable Command Violation: The disabled 'convenience_type' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Realm: /Realm/ObjectServerTests/SwiftObjectServerTests.swift:2393:26: warning: Blanket Disable Command Violation: The disabled 'multiple_closures_with_trailing_closure' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Realm: /RealmSwift/Tests/PrimitiveMapTests.swift:23:22: warning: Blanket Disable Command Violation: The disabled 'cyclomatic_complexity' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Realm: /RealmSwift/Tests/QueryTests.swift:23:22: warning: Blanket Disable Command Violation: The disabled 'large_tuple' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Realm: /RealmSwift/Tests/QueryTests.swift:23:34: warning: Blanket Disable Command Violation: The disabled 'vertical_parameter_alignment' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Realm: /RealmSwift/Tests/RealmCollectionTypeTests.swift:19:22: warning: Blanket Disable Command Violation: The disabled 'type_name' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in SourceKitten: /Source/SourceKittenFramework/File.swift:8:34: warning: Blanket Disable Command Violation: The disabled 'type_body_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in SourceKitten: /Source/SourceKittenFramework/StatementKind.swift:1:22: warning: Blanket Disable Command Violation: The disabled 'identifier_name' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in SourceKitten: /Source/SourceKittenFramework/library_wrapper_Clang_C.swift:7:22: warning: Blanket Disable Command Violation: The disabled 'unused_declaration' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in SourceKitten: /Source/SourceKittenFramework/library_wrapper_SourceKit.swift:10:22: warning: Blanket Disable Command Violation: The disabled 'unused_declaration' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryRuntime/Sources/Generated/AutoHashable.generated.swift:3:22: warning: Blanket Disable Command Violation: The disabled 'all' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryRuntime/Sources/Generated/Coding.generated.swift:3:22: warning: Blanket Disable Command Violation: The disabled 'vertical_whitespace' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryRuntime/Sources/Generated/Coding.generated.swift:3:42: warning: Blanket Disable Command Violation: The disabled 'trailing_newline' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryRuntime/Sources/Generated/Description.generated.swift:3:22: warning: Blanket Disable Command Violation: The disabled 'vertical_whitespace' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryRuntime/Sources/Generated/Equality.generated.swift:3:22: warning: Blanket Disable Command Violation: The disabled 'vertical_whitespace' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryRuntime/Sources/Generated/JSExport.generated.swift:3:22: warning: Blanket Disable Command Violation: The disabled 'vertical_whitespace' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryRuntime/Sources/Generated/JSExport.generated.swift:3:42: warning: Blanket Disable Command Violation: The disabled 'trailing_newline' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryRuntime/Sources/Generated/Typed.generated.swift:3:22: warning: Blanket Disable Command Violation: The disabled 'vertical_whitespace' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryTests/Generating/SwiftTemplateSpecs.swift:23:26: warning: Blanket Disable Command Violation: The disabled 'function_body_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryTests/GeneratorSpec.swift:14:26: warning: Blanket Disable Command Violation: The disabled 'function_body_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryTests/Models/TypedSpec.generated.swift:13:22: warning: Blanket Disable Command Violation: The disabled 'function_body_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryTests/Parsing/ComposerSpec.swift:18:22: warning: Blanket Disable Command Violation: The disabled 'type_body_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryTests/Parsing/ComposerSpec.swift:20:26: warning: Blanket Disable Command Violation: The disabled 'function_body_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryTests/Parsing/FileParser + MethodsSpec.swift:15:26: warning: Blanket Disable Command Violation: The disabled 'function_body_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryTests/Parsing/FileParserSpec.swift:14:26: warning: Blanket Disable Command Violation: The disabled 'function_body_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /SourceryTests/SourcerySpec.swift:15:34: warning: Blanket Disable Command Violation: The disabled 'type_body_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /Templates/Tests/Context/AutoLenses.swift:28:22: warning: Blanket Disable Command Violation: The disabled 'identifier_name' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /Templates/Tests/Generated/AutoHashable.generated.swift:3:22: warning: Blanket Disable Command Violation: The disabled 'all' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /Templates/Tests/Generated/AutoLenses.generated.swift:3:22: warning: Blanket Disable Command Violation: The disabled 'variable_name' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /Templates/Tests/Generated/AutoMockable.generated.swift:3:22: warning: Blanket Disable Command Violation: The disabled 'line_length' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Sourcery: /Templates/Tests/Generated/AutoMockable.generated.swift:4:22: warning: Blanket Disable Command Violation: The disabled 'variable_name' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Wire: /Wire-iOS/Generated/Assets+Generated.swift:1:22: warning: Blanket Disable Command Violation: The disabled 'all' rule should be re-enabled before the end of the file (blanket_disable_command)
:warning: This PR introduced a violation in Wire: /Wire-iOS/Generated/Strings+Generated.swift:1:22: warning: Blanket Disable Command Violation: The disabled 'all' rule should be re-enabled before the end of the file (blanket_disable_command)
18 Messages
:book: Linting Aerial with this PR took 1.04s vs 1.04s on main (0% slower)
:book: Linting Alamofire with this PR took 1.34s vs 1.34s on main (0% slower)
:book: Linting Brave with this PR took 7.27s vs 7.25s on main (0% slower)
:book: Linting DuckDuckGo with this PR took 3.11s vs 3.1s on main (0% slower)
:book: Linting Firefox with this PR took 9.17s vs 9.12s on main (0% slower)
:book: Linting Kickstarter with this PR took 10.07s vs 10.12s on main (0% faster)
:book: Linting Moya with this PR took 0.52s vs 0.53s on main (1% faster)
:book: Linting NetNewsWire with this PR took 3.02s vs 3.01s on main (0% slower)
:book: Linting Nimble with this PR took 0.6s vs 0.58s on main (3% slower)
:book: Linting PocketCasts with this PR took 7.07s vs 7.07s on main (0% slower)
:book: Linting Quick with this PR took 0.23s vs 0.22s on main (4% slower)
:book: Linting Realm with this PR took 11.32s vs 11.33s on main (0% faster)
:book: Linting SourceKitten with this PR took 0.43s vs 0.43s on main (0% slower)
:book: Linting Sourcery with this PR took 2.18s vs 2.18s on main (0% slower)
:book: Linting Swift with this PR took 4.75s vs 4.75s on main (0% slower)
:book: Linting VLC with this PR took 1.35s vs 1.33s on main (1% slower)
:book: Linting Wire with this PR took 8.22s vs 8.19s on main (0% slower)
:book: Linting WordPress with this PR took 10.98s vs 11.0s on main (0% faster)

Generated by :no_entry_sign: Danger

SwiftLintBot avatar Feb 08 '23 01:02 SwiftLintBot

In general, I like this rule a lot! The OSS findings and the findings in SwiftLint itself splendidly show how much SwiftLint misses it.

SimplyDanny avatar Feb 28 '23 19:02 SimplyDanny

In general, I like this rule a lot! The OSS findings and the findings in SwiftLint itself splendidly show how much SwiftLint misses it.

Thanks! I just saw so many people just disabling stuff for the remainder of the file to fix one warning that it got annoying - we have a custom Danger script to do (some of) this, but much nicer to just have it baked in. And yes, it does seem to get a lot of hits :-)

Not entirely sure that no_blanket_disable is the best naming. Possibly blanket_disable_command, to echo superfluous_disable_command?

mildm8nnered avatar Feb 28 '23 23:02 mildm8nnered

Indeed, blanket_disable_command sounds better to me. Also, the wording of the violation messages can be improved. I made a few suggestions. Please let me know what you think.

Cool - I went with blanket_disable_command and all your reason suggestions.

mildm8nnered avatar Mar 02 '23 00:03 mildm8nnered

Just found some more points I'd like to discuss. Sorry! 😅

No worries :-) I'll try and get to these today ...

mildm8nnered avatar Mar 05 '23 13:03 mildm8nnered

Just found some more points I'd like to discuss. Sorry! 😅

No worries :-) I'll try and get to these today ...

ok - so I think I've addressed everything from that last batch :-)

mildm8nnered avatar Mar 05 '23 16:03 mildm8nnered

Well done. Thanks @mildm8nnered!

SimplyDanny avatar Mar 07 '23 20:03 SimplyDanny