https://kotlinlang.org logo
#intellij-tricks
Title
# intellij-tricks
m

Manuel Dossinger

11/18/2023, 7:06 AM
I have a method that is totally redundant to call multiple times. Think
Copy code
doResetWithValue(5)
doResetWithValue(5)
but with less obvious names. Is there a way to get an inspection warn me about this pattern? Similar to "redundant assignment" but with my method.
r

Raimund Klein

11/18/2023, 7:28 AM
x

xoangon

11/18/2023, 7:32 AM
You can also use the "Replace structurally" option and then convert that rule into an inspection
m

Manuel Dossinger

11/18/2023, 2:37 PM
@Raimund Klein now that you said it, actually the example should read something like
Copy code
doResetWithValue(7)
doResetWithValue(5)
(the arguments are different, but the second invocation trumps the first one rendering it useless)
@xoangon I tried the strucutally replace option, but was not able to get it to match these two subsequent method invocations. I'd be happy about a hint there 🙂
x

xoangon

11/18/2023, 4:42 PM
I need more information. May those repeated invocations happen in the same scope (say, for example, the same method)? Are those invocations always consecutive?
m

Manuel Dossinger

11/18/2023, 5:47 PM
No, not consecutive. Same method/scope yes, but not in different branches. I thought it would be easier to start with consecutive invocations. So the following would be ok and should not be caught by the inspection: if(condition) doResetWithValue(5) else doResetWithValue(7)