realm / SwiftLint

A tool to enforce Swift style and conventions.
https://realm.github.io/SwiftLint
MIT License
18.7k stars 2.23k forks source link

attributes rule clashes with `// swiftlint:disable` comments #4568

Closed rist closed 2 years ago

rist commented 2 years ago

New Issue Checklist

Describe the bug

For a function which has an attribute (e.g. @objc) the attribute warning gets triggered when adding a comment - e.g. // swiftlint:disable:next function_body_length

Complete output when running SwiftLint, including the stack trace and command used

So with this file

import UIKit

class SettingsViewController: TableViewController {

    @objc
    // swiftlint:disable:next function_body_length
    fileprivate func setupDataSource() {
        print("hello")
        print("again")
        print("one more")
        return
    }
}

I get either

swiftlint
Linting Swift files in current working directory
Linting 'Test.swift' (1/1)
/Users/rist/Desktop/tmp/Test.swift:11:17: warning: Attributes Violation: Attributes should be on their own lines in functions and types, but on the same line as variables and imports. (attributes)
Done linting! Found 1 violation, 0 serious in 1 file.

or

swiftlint
Linting Swift files in current working directory
Linting 'Test.swift' (1/1)
/Users/rist/Desktop/tmp/Test.swift:10:22: warning: Function Body Length Violation: Function body should span 2 lines or less excluding comments and whitespace: currently spans 4 lines (function_body_length)
Done linting! Found 1 violation, 0 serious in 1 file.

depending if I add the line containing swiftlint:disable:next or not. I also tried placing the SwiftLint comment above @objc but this also didn't work

I configured SwiftLint like this to keep the example size small

function_body_length:
  - 2 # warning
  - 4 # error

Environment

opt_in_rules:
  - attributes

function_body_length:
  - 2 # warning
  - 4 # error
khoogheem commented 2 years ago

Looks like there is also another issue with Attributes: The below triggers Attributes should be on their own lines in functions and types, but on the same line as variables and imports even if my config says:

attributes:
  always_on_same_line: ["@IBAction", "@IBDesignable", "@NSManaged", "@objc"]
  always_on_line_above: ["@discardableResult", "@WidgetBundleBuilder"]
 @available(iOSApplicationExtension 14.0, *)
 @WidgetBundleBuilder
 static var includedWidgets: some Widget {
        ...widget
        ...widget
 }

Now this works as expected

 @WidgetBundleBuilder
 static var includedWidgets: some Widget {
        ...widget
        ...widget
 }
jpsim commented 2 years ago

@rist you have a few options for how to structure this:

@objc // swiftlint:disable:next function_body_length
fileprivate func setupDataSource() {

or

@objc
fileprivate func setupDataSource() {
// swiftlint:disable:previous function_body_length

or

@objc
fileprivate func setupDataSource() { // swiftlint:disable:this function_body_length

or

// swiftlint:disable function_body_length
@objc
fileprivate func setupDataSource() {
...
// swiftlint:enable function_body_length
rist commented 2 years ago

oh wow I'll use the first option as it's most in line with my other settings

thanks for explaining and these examples