saveourtool / diktat

Strict coding standard for Kotlin and a custom set of rules for detecting code smells, code style issues and bugs
https://diktat.saveourtool.com
MIT License
521 stars 39 forks source link

Incorrect linting for single-expression functions. #471

Closed Animeshz closed 3 years ago

Animeshz commented 3 years ago

Describe the bug

Incorrect [WRONG_NEWLINES] and [WRONG_INDENTATION] lint warnings generated for single-expression functions

Here's an example of the this bug with the reported warnings: image

Expected behavior

There should not be any warning.

Observed behavior

[WRONG_NEWLINES] and [WRONG_INDENTATION] lint warnings are generated.

Steps to Reproduce

  1. Copy the following reproducible code:
    fun absolute(x: Int) =
    if (x > 0) x
    else -x
  2. Run the diktat linter with default configurations.

Environment information

petertrr commented 3 years ago

Hi @Animeshz , thanks for your feedback!

Regarding indentation errors - could you try to set extendedIndentAfterOperators and extendedIndentBeforeDot to false in indentation configuration, like this:

- name: WRONG_INDENTATION
  enabled: true
  configuration:
    extendedIndentAfterOperators: false
    extendedIndentBeforeDot: false

These parameters are true by default, so diktat expects 8 spaces after = and 8 spaces before each dot-qualified call. Does it help or are there still unexpected errors?

Regarding WRONG_NEWLINES - these errors are correct according to diktat code style, see rule about line breaks. So, .filterIsInstance call should start on a new line because this is a chain of dot-qualified calls, and same goes for the line }.catch { logger.catching(it) } - diktat tells you to insert a line break before .catch too.