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

False-positive MagicNumber issue reported when using negative numbers #530

Closed
xrigau opened this issue Oct 31, 2017 · 5 comments · Fixed by #2315
Closed

False-positive MagicNumber issue reported when using negative numbers #530

xrigau opened this issue Oct 31, 2017 · 5 comments · Fixed by #2315
Milestone

Comments

@xrigau
Copy link

xrigau commented Oct 31, 2017

It seems that detekt is reporting a false-positive MagicNumber issue when defining a negative number as a constant.

Code to reproduce:

package com.foo.bar

class Test {
    companion object {
        private const val FALSE_POSITIVE = -180
        private const val THIS_IS_FINE = 180
    }
}

Running ./gradlew detektCheck reports the following issue:

Ruleset: style
        MagicNumber - [FALSE_POSITIVE] at com/foo/bar/Test.kt:12:43

Using the annotation as expected (without raising the issue):

    companion object {
        @Suppress("MagicNumber")
        private const val THIS_WORKS = -180
    }

Currently using version 1.0.0.RC4-3 but I also tried with the latest 1.0.0.RC5-3 and got the same results.
Relevant configuration:

  MagicNumber:
    active: true
    ignoreNumbers: '-1,0,1,2'
    ignoreHashCodeFunction: true
    ignorePropertyDeclaration: false
    ignoreAnnotation: true

I also tried adding extra options:

    ignoreCompanionObjectPropertyDeclaration: true
    ignoreConstantDeclaration: true

But it didn't make a difference

@rock3r
Copy link
Contributor

rock3r commented Oct 31, 2017

This is probably hitting a corner case in the companion object/constants detection... It should work (and negative numbers are covered by the logic). Will maybe take a look at some point if I have some spare time over the next few days.

@arturbosch
Copy link
Member

arturbosch commented Nov 1, 2017

Just checked fast, but could not reproduce it. Not in a test case not by running the cli.

	given("false positive negative constant number") {
		it("test") {
			val code = """
				package com.foo.bar

				class Test {
					companion object {
						private const val FALSE_POSITIVE = -180
						private const val THIS_IS_FINE = 180
					}
				}
			"""
			assertThat(MagicNumber().lint(code)).isEmpty()
		}
	}

@arturbosch
Copy link
Member

Feel free to reopen this if the issue still happening for you from a master build.

@xrigau
Copy link
Author

xrigau commented Nov 13, 2017

sorry I've been AFK for a few days, will have a look again and reopen if the issue is still there, thanks!

@lock
Copy link

lock bot commented Jun 20, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related topics.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 20, 2019
@arturbosch arturbosch added this to the 1.6.0 milestone Feb 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants