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

indentation_width warning when preprocessor statements are used #4030

Closed
2 tasks done
phranck opened this issue Jul 20, 2022 · 2 comments · Fixed by #4066
Closed
2 tasks done

indentation_width warning when preprocessor statements are used #4030

phranck opened this issue Jul 20, 2022 · 2 comments · Fixed by #4066
Assignees
Labels
bug Unexpected and reproducible misbehavior.

Comments

@phranck
Copy link

phranck commented Jul 20, 2022

New Issue Checklist

Describe the bug

With enabled rule indentation_width you will get warnings for preprocessor statements in nested structures. Xcode's cmd + i formats the code correctly (preprocessor statements always at the beginning of a line, all other commands, view modifiers etc. indented as expected.

Code Snippet

private struct MyView: View {
#if os(iOS)
    @State var scrollViewHeight: CGFloat = .zero
#endif
    @EnvironmentObject var config: MyConfig

    var body: some View {
#if os(iOS)
        ScrollView {                          // <- This triggers warning
            content
        }
        .frame(maxHeight: scrollViewHeight)
#else
        content                               // <- This triggers warning
#endif
    }
}
Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint

Warning Output

Indentation Width Violation: Code should be indented using one tab or 4 spaces. (indentation_width)

Environment

  • SwiftLint version (run swiftlint version to be sure)? - 0.47.1
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? - SPM Build Tool Plugin
  • Paste your configuration file:
excluded:

disabled_rules:
  - identifier_name
  - notification_center_detachment
  - orphaned_doc_comment
  - statement_position
  - todo
  - unused_capture_list

opt_in_rules:
  - array_init
  - attributes
  - closure_end_indentation
  - closure_spacing
  - collection_alignment
  - colon
  - convenience_type
  - discarded_notification_center_observer
  - discouraged_object_literal
  - empty_collection_literal
  - empty_count
  - empty_string
  - enum_case_associated_values_count
  - explicit_init
  - fatal_error_message
  - first_where
  - force_unwrapping
  - identical_operands
  - implicitly_unwrapped_optional
  - indentation_width
  - last_where
  - leading_whitespace
  - legacy_random
  - literal_expression_end_indentation
  - modifier_order
  - multiline_arguments
  - multiline_function_chains
  - multiline_literal_brackets
  - multiline_parameters
  - multiline_parameters_brackets
  - number_separator
  - operator_usage_whitespace
  - pattern_matching_keywords
  - prefer_self_type_over_type_of_self
  - redundant_nil_coalescing
  - redundant_type_annotation
  - required_deinit
  - sorted_imports
  - static_operator
  - strict_fileprivate
  - switch_case_alignment
  - switch_case_on_newline
  - toggle_bool
  - trailing_closure
  - unavailable_function
  - unneeded_parentheses_in_closure_argument
  - vertical_whitespace_closing_braces
  - vertical_whitespace_opening_braces
  - yoda_condition


analyzer_rules:
  - unused_declaration
  - unused_import


custom_rules:
  array_constructor:
    name: "Array/Dictionary initializer"
    regex: '[let,var] .+ = (\[.+\]\(\))'
    capture_group: 1
    message: "Use explicit type annotation when initializing empty arrays and dictionaries"
    severity: warning


force_cast: warning
force_try: warning
function_body_length:
    warning: 60

legacy_hashing: error

identifier_name:
  excluded:
    - i
    - id
    - x
    - y
    - z

line_length:
  warning: 140
  error: 240
  ignores_urls: true
  ignores_function_declarations: true
  ignores_comments: true

multiline_arguments:
  first_argument_location: next_line
  only_enforce_after_first_closure_on_first_line: true

private_over_fileprivate:
  validate_extensions: true

trailing_whitespace:
  ignores_empty_lines: false
  ignores_comments: true

vertical_whitespace:
  max_empty_lines: 1

reporter: "xcode"
  • Are you using nested configurations? - NO
  • Which Xcode version are you using (check xcodebuild -version)?
Xcode 14.0
Build version 14A5270f
  • Do you have a sample that shows the issue? - see example above
@SimplyDanny SimplyDanny added the bug Unexpected and reproducible misbehavior. label Jul 21, 2022
@SimplyDanny SimplyDanny self-assigned this Jul 21, 2022
@SimplyDanny
Copy link
Collaborator

There is now a new option include_compiler_directives you can set to false. This is supposed to silence the warnings in your example.

@phranck
Copy link
Author

phranck commented Aug 16, 2022

Thanks @SimplyDanny! You are right, it works for something like in my example.
Unfortunately it doesn't work for something like this (maybe for extensions):

public extension Font {
    static var cuLargeTitle: Font {
#if os(iOS)
        Font.system(size: UIFontMetrics.default.scaledValue(for: 32), weight: .heavy)   // triggers warning
#elseif os(macOS)
        Font.system(size: 26, weight: .bold)   // triggers warning
#endif
    }

    static var cuTitle: Font {
#if os(iOS)
        Font.system(size: UIFontMetrics.default.scaledValue(for: 26), weight: .bold)   // triggers warning
#elseif os(macOS)
        Font.system(size: 22, weight: .semibold)   // triggers warning
#endif
    }

…
}

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected and reproducible misbehavior.
Projects
None yet
2 participants