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

VARIABLE_NAME_INCORRECT_FORMAT false positive when a property has a backing field #1711

Closed
1 task done
Tracked by #1709
0x6675636b796f75676974687562 opened this issue Jul 14, 2023 · 0 comments · Fixed by #1810
Closed
1 task done
Tracked by #1709
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:4:17: [VARIABLE_NAME_INCORRECT_FORMAT] variable name should be in lowerCamelCase and should contain only latin (ASCII) letters or numbers and should start from lower letter: _table (diktat-ruleset:identifier-naming)

Probably we should make an exception for backing properties whenever there's an explicit backing field with the corresponding name (xyz_xyz) and of the same type.

Requirements:

  1. the backing field should be private (or have a lower visibility than that of the property);
  2. the backing field should have a matching name (xyz_xyz);
  3. the backing field should have the same type as the property (either convert both types to a simple name, or resolve to a FQN before you compare; nullable subtypes should be permitted: Map<String, Int>Map<String, Int>?);
  4. the backing field should have no accessors/modifiers of its own;
  5. the property should have at least an accessor, or a modifier, or both.

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
diphtongue added a commit that referenced this issue Nov 20, 2023
- fixed warning `VARIABLE_NAME_INCORRECT_FORMAT` on `backing field`.
- added warning test.
Closes #1711
diphtongue added a commit that referenced this issue Nov 20, 2023
diphtongue added a commit that referenced this issue Nov 21, 2023
- added `ASTNode.isCorrectBackingField(ASTNode)` method.
- added warning tests, which trigger on incorrect `backing field` format.

Closes #1711
@nulls nulls added this to the 2.0.0 milestone Nov 22, 2023
diphtongue added a commit that referenced this issue Nov 22, 2023
… has a backing field (#1810)

### What's done:
- fixed warning `VARIABLE_NAME_INCORRECT_FORMAT` on `backing field`.
- added `ASTNode.isCorrectBackingField(ASTNode)` method.
- added warning tests, which trigger on incorrect `backing field` format.

Closes #1711
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
3 participants