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

CUSTOM_GETTERS_SETTERS false positive when a property has a backing field #1709

Closed
1 task done
Tracked by #1711
0x6675636b796f75676974687562 opened this issue Jul 14, 2023 · 0 comments · Fixed by #1815
Closed
1 task done
Tracked by #1711
Assignees
Labels
bug Something isn't working good first issue Good for newcomers
Milestone

Comments

@0x6675636b796f75676974687562
Copy link
Member

0x6675636b796f75676974687562 commented Jul 14, 2023

Describe the bug

Consider this idiomatic example taken directly from https://kotlinlang.org:

package com.example

class MutableTableContainer {
    private var _table: Map<String, Int>? = null

    val table: Map<String, Int>
        get() {
            if (_table == null) {
                _table = hashMapOf()
            }
            return _table ?: throw AssertionError("Set to null by another thread")
        }
}

Diktat 1.2.5 warns:

MutableTableContainer.kt:7:9: [CUSTOM_GETTERS_SETTERS] custom getters and setters are not recommended, use class methods instead: get (cannot be auto-corrected) (diktat-ruleset:custom-getter-setter)

Probably we should make an exception for property accessors/modifiers whenever there's a backing field with the corresponding name (xyz_xyz) and of the same type.

Related:

Expected behavior

No warning should have been reported.

Observed behavior

A warning was reported.

Steps to Reproduce

Check the above code example with Diktat.

Environment information

  • diktat version: 1.2.5
  • build tool (maven/gradle): any
  • how is diktat run (CLI, plugin, etc.): doesn't matter
  • kotlin version: 1.9
  • operating system: any
  • link to a project (if your project is public):
@0x6675636b796f75676974687562 0x6675636b796f75676974687562 added the bug Something isn't working label Jul 14, 2023
@nulls nulls added this to the 2.0.0 milestone Nov 22, 2023
diphtongue added a commit that referenced this issue Nov 24, 2023
…ield.

- added `ASTNode.isPrivate()`, `ASTNode.hasSetterGetter()` methods
- added `isPairPropertyBackingField(propertyNode: ASTNode?, backingFieldNode: ASTNode?)` function
- added warning tests.

Closes #1709
diphtongue added a commit that referenced this issue Nov 24, 2023
diphtongue added a commit that referenced this issue Nov 27, 2023
diphtongue added a commit that referenced this issue Nov 27, 2023
… field (#1815)

### What's done:

- fixed warning `CUSTOM_GETTERS_SETTERS` on properties with backing field.
- added `ASTNode.isPrivate()`, `ASTNode.hasSetterGetter()` methods
- added `isPairPropertyBackingField(propertyNode: ASTNode?, backingFieldNode: ASTNode?)` function
- added warning tests.

Closes #1709
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants