realm / SwiftLint

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

'implicit_return' rule config not working #3029

Closed christopher-hof closed 4 years ago

christopher-hof commented 4 years ago

New Issue Checklist

Describe the bug

I'm trying to configure the 'implicit_return' so it does not trigger for functions (only for 'getter' and 'closures'). But the following configuration does not seem to work.

implicit_return:
  excluded:
    - function

Environment

excluded:
  - Carthage
line_length: 140
type_body_length: 500
file_length: 600
type_name:
  max_length:
    warning: 50
identifier_name:
    excluded:
        - id
        - to
opt_in_rules:
    - array_init
    - attributes
    - closure_spacing
    - collection_alignment
    - contains_over_first_not_nil
    - contains_over_range_nil_comparison
    - discouraged_object_literal
    - empty_count
    - empty_string
    - empty_xctest_method
    - enum_case_associated_value_count
    - explicit_init
    - fatal_error_message
    - first_where
    - flatmap_over_map_reduce
    - force_unwrapping
    - function_default_parameter_at_end
    - identical_operands
    - implicit_return
    - joined_default_parameter
    - legacy_random
    - last_where
    - let_var_whitespace
    - literal_expression_end_indentation
    - lower_acl_than_parent
    - modifier_order
    - multiline_arguments
    - multiline_function_chains
    - multiline_parameters
    - operator_usage_whitespace
    - optional_enum_case_matching
    - overridden_super_call
    - override_in_extension
    - pattern_matching_keywords
    - prefer_self_type_over_type_of_self
    - prohibited_super_call
    - reduce_into
    - redundant_nil_coalescing
    - required_enum_case
    - single_test_class
    - sorted_first_last
    - sorted_imports
    - static_operator
    - toggle_bool
    - unavailable_function
    - untyped_error_in_catch
    - unused_control_flow_label
    - unused_declaration
    - unused_import
    - vertical_parameter_alignment
    - yoda_condition

disabled_rules:
    - force_cast

implicit_return:
    excluded:
        - function
// This triggers a violation:
private func trimValue(_ value: String?) -> String {
    return (value ?? "").trimmingCharacters(in: CharacterSet.whitespaces)
}
marcelofabri commented 4 years ago

The rule supports an included key (not excluded)

christopher-hof commented 4 years ago
implicit_return:
    included:
        - closure
        - getter

also doesn't seem to work.

// This still triggers a violation
private func trimValue(_ value: String?) -> String {
    return (value ?? "").trimmingCharacters(in: CharacterSet.whitespaces)
}
olvrlrnz commented 4 years ago

I have the same problem and did some more research, but don't really understand the problem.

I checked out Yams and added a new unit test:

let string = try String(contentsOfFile: "/tmp/swiftlint.yml")

 guard let yaml = try Yams.load(yaml: string) as? [String: Any],
       let ruleConfig = yaml["implicit_return"] as? [String: Any] else {
           throw NSError()
 }

var rule = ImplicitReturnConfiguration()
try rule.apply(configuration: ruleConfig)

XCTAssertTrue(rule.includedKinds.contains(.getter))
XCTAssertTrue(rule.includedKinds.contains(.closure))
XCTAssertFalse(rule.includedKinds.contains(.function))
XCTAssertTrue(rule.severityConfiguration.severity == .warning)

Config file:

implicit_return:
  severity: warning
  included:
    - getter
    - closure

or with default severity:

implicit_return:
  included:
    - getter
    - closure

the unit test passed for both inputs. However, swiftlint rules only works for the first config file, otherwise it outputs

Loading configuration from '.swiftlint.yml'
Invalid configuration for 'implicit_return'. Falling back to default.

However, no matter what you put in the config file, the implicit_return rule doesn't seem to care much about the config anyway. In swiftlint rules it always reports: | warning | but according to the source code it should also tell you what ReturnKinds are currently active the default

mattrubin commented 4 years ago

I'm using the configuration:

implicit_return:
  included:
    - closure

Most of the time, the rule behaves as expected, but occasionally, I'll get warnings for function and getter returns. I haven't yet been able to figure out if there's some environmental change that's causing some of the lint runs to obey the configuration and some to not, or if it's truly nondeterministic.

I'm not sure if it's relevant, but swiftlint rules always shows the configuration as

warning, included: [SwiftLintFramework.ImplicitReturnConfiguration.ReturnKind.closure, SwiftLintFramework.ImplicitReturnConfiguration.ReturnKind.function, SwiftLintFramework.ImplicitReturnConfiguration.ReturnKind.getter]
Igor-Palaguta commented 4 years ago

Looks like after changing implicit_return configuration in yaml file we should run once swiftlint --no-cache

StevenSorial commented 4 years ago

Still not working

christopher-hof commented 4 years ago

Release 0.40.0 fixed this issue.

implicit_return:
    included:
        - closure
        - getter

now works as intended.

cprovatas commented 3 years ago

Using a config still doesn't work for me in 0.42.0

cprovatas commented 3 years ago

It looks like ImplicitReturnConfiguration is initialized without passing any config and just uses the default arguments?

https://github.com/realm/SwiftLint/blob/5d6e25ae5fb25a24eafa7d2d3d8d0d2efcef46bf/Source/SwiftLintFramework/Rules/Style/ImplicitReturnRule.swift#L5

jpsim commented 3 years ago

@cprovatas all rules are initialized with some default configuration, and then that configuration is mutated with the overrides from your configuration file so that's expected.

cprovatas commented 3 years ago

I got it working now. Thanks