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

Unsigned integer literal is perhaps incorrectly marked as a MAGIC_NUMBER #1224

Closed
michaelkeeling opened this issue Mar 4, 2022 · 2 comments · Fixed by #1276
Closed

Unsigned integer literal is perhaps incorrectly marked as a MAGIC_NUMBER #1224

michaelkeeling opened this issue Mar 4, 2022 · 2 comments · Fixed by #1276
Assignees
Labels
bug Something isn't working
Milestone

Comments

@michaelkeeling
Copy link

michaelkeeling commented Mar 4, 2022

Describe the bug

Unsigned integer literal is perhaps incorrectly marked as a MAGIC_NUMBER

Expected behavior

The rules for unsigned numeric values should be the same as the signed counterparts.

Observed behavior

Unsigned integer literal is incorrectly marked as a MAGIC_NUMBER

[MAGIC_NUMBER] avoid using magic numbers, instead define
constants with clear names describing what the magic number means: 0U (cannot be auto-corrected)

Steps to Reproduce

Assign an unsigned integer to a variable.

// this raises a MAGIC_NUMBER warning
var positiveNumber = 0U

// this does not have a warning
var otherNumber = 0

Environment information

  • diktat version: 1.0.1
  • build tool (maven/gradle): gradle
  • how is diktat run (CLI, plugin, etc.): gradle plugin
  • kotlin version: 1.6.10
  • operating system: MacOS 12.2.1
  • link to a project (if your project is public): N/A
@michaelkeeling michaelkeeling added the bug Something isn't working label Mar 4, 2022
@michaelkeeling michaelkeeling changed the title Unsigned integer literal is perhaps incorrectly marked as a MAGIC_NUMBER Unsigned integer literal is perhaps incorrectly marked as a MAGIC_NUMBER Mar 4, 2022
@orchestr7
Copy link
Member

orchestr7 commented Apr 27, 2022

Oh, looks like we missed this, we only added workarounds for 0,1,2. Should add the same for u, l.
Before the release you can workaround it with a @Suppress annotation.

@michaelkeeling, thank you for this report, sorry that we missed this issue, because of the current situation :(
Will be fixed in the nearest release!

@orchestr7 orchestr7 added this to the 1.1.1 milestone Apr 27, 2022
Cheshiriks added a commit that referenced this issue Apr 28, 2022
### What's done:
* fixed ignore number in MAGIC_NUMBER rule
Closes #1224
Cheshiriks added a commit that referenced this issue Apr 28, 2022
### What's done:
* fixed ignore number in MAGIC_NUMBER rule
Closes #1224
@petertrr
Copy link
Member

petertrr commented Apr 28, 2022

Before the release you can workaround it with a @Suppress annotation.

Actually, we already support setting custom numbers allowed by this rule. In diktat.yaml in configuration section of MAGIC_NUMBER rule they can be set as a comma-separated list in ignoreNumbers field:

- name: MAGIC_NUMBER
  enabled: true
  configuration:
    ignoreNumbers: 1,2,0U,1U,2U

But sure, literals for longs and unsigned numbers will be supported out-of-the-box as well.

Cheshiriks added a commit that referenced this issue Apr 28, 2022
### What's done:
* fixed ignore number in MAGIC_NUMBER rule
Closes #1224
Cheshiriks added a commit that referenced this issue Apr 28, 2022
### What's done:
* fixed ignore number in MAGIC_NUMBER rule
Closes #1224
Cheshiriks added a commit that referenced this issue Apr 28, 2022
### What's done:
* fixed ignore number in MAGIC_NUMBER rule
Closes #1224
Cheshiriks added a commit that referenced this issue Apr 28, 2022
### What's done:
* fixed ignore number in MAGIC_NUMBER rule
Closes #1224
Cheshiriks added a commit that referenced this issue Apr 28, 2022
### What's done:
* fixed ignore number in MAGIC_NUMBER rule
Closes #1224
Cheshiriks added a commit that referenced this issue Apr 28, 2022
### What's done:
* fixed ignore number in MAGIC_NUMBER rule
Closes #1224
Cheshiriks added a commit that referenced this issue Apr 28, 2022
…BER (#1276)

* Unsigned integer literal is perhaps incorrectly marked as a MAGIC_NUMBER

### What's done:
* fixed ignore number in MAGIC_NUMBER rule
Closes #1224
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants