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

ignoreNamedArguments breaks marking non-named magic number params #659

Closed
lwasylkowski opened this issue Jan 3, 2018 · 4 comments
Closed
Assignees
Milestone

Comments

@lwasylkowski
Copy link

The following test added in MagicNumberSpec fails:

given("ignoring named arguments is off") {

    fun code(numberString: String) = compileContentForTest("""
        data class Model(
                val someVal: Int,
                val other: String = "default"
        )

        var model = Model($numberString)
    """)

    it("should not ignore int") {
        val rule = MagicNumber(TestConfig(mapOf("ignoreNamedArgument" to "true")))
        assertThat(rule.lint(code("53"))).hasSize(1)
    }
}

It looks like this behaviour is not correct, since Model($numberString) will resolve to a non-named argument being passed a magic number. ignoreNamedArguments option set to true should not affect lint result in this case, but it does. In our codebase ignoreNamedArgument: true makes magic numbers like this:

internal fun String.appendRandomDigit() = plus(Random().nextInt(10))

go undetected

@arturbosch
Copy link
Member

How old is your branch? Your not on the master branch, are you?
There is no such test case in master.

	given("ignoring named arguments") {
		given("in constructor invocation") {
			fun code(numberString: String) = compileContentForTest("""
				data class Model(
						val someVal: Int,
						val other: String = "default"
				)

				var model = Model(someVal = $numberString)
			""")

			it("should not ignore int by default") {
				assertThat(MagicNumber().lint(code("53"))).hasSize(1)
			}
....

Sorry for the late response.

@lwasylkowski
Copy link
Author

lwasylkowski commented Jan 8, 2018

Right, there's no such test - I've added it to show that the rule is behaving incorrectly.

The test you've pasted checks that if ignoreNamedArgument = false then named number is not ignored.

The test I've added checks whether if ignoreNamedArgument = true then unnamed number is catched, but it fails. But ignoreNamedArgument should have no effect on unnamed arguments, right? Essentially once someone sets ignoreNamedArguments = true in detekt config, then all number parameters are ignored by the magic number rule, named or not

@arturbosch
Copy link
Member

arturbosch commented Jan 9, 2018

Oh, misunderstood. Yes, your right. I verified the wrong behaviour with your test case.

Edit: Thanks for reporting. Fixed.

@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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants