Inspection question

IntellijIdea 8.1.3

Following code:
        int n=1;
        f(n++);
        f(n++);
        f(n++);
generates inspection warning "The value changed at 'n++' is never used".
In fact, 'n' is used: as a function parameter.

JetBrains, could you fix this minor inconvenience? In the spare time, of
course :)

TomP

6 comments
Comment actions Permalink

Hello TomP,

The value of "n" is used, but the result of the increment operation is not.
The meaning of the code will not change if you replace the last n++ with
simply n.

IntellijIdea 8.1.3

Following code:
int n=1;
f(n++);
f(n++);
f(n++);
generates inspection warning "The value changed at 'n++' is never
used".
In fact, 'n' is used: as a function parameter.

JetBrains, could you fix this minor inconvenience? In the spare time,
of course :)

TomP

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

I don't agree.
I frequently use this pattern in the following scenario:
PreparedStatement sqlStatement = connection.prepareStatement(sql);
int n = 1;
sqlStatement.setString(n++, paramA);
sqlStatement.setString(n++, paramB);
sqlStatement.setString(n++, paramC);
instead of
sqlStatement.setString(1, paramA);
sqlStatement.setString(2, paramB);
sqlStatement.setString(3, paramC);
Now, if I want to change the order of assigning parameters paramB and
paramC, then I just change the order of lines in the code to:
sqlStatement.setString(n++, paramA);
sqlStatement.setString(n++, paramC);
sqlStatement.setString(n++, paramB);
instead of error prone code:
sqlStatement.setString(1, paramA);
sqlStatement.setString(3, paramB);
sqlStatement.setString(2, paramC);

TomP

Dmitry Jemerov wrote:

Hello TomP,

The value of "n" is used, but the result of the increment operation is
not. The meaning of the code will not change if you replace the last n++
with simply n.

>> IntellijIdea 8.1.3
>>
>> Following code:
>> int n=1;
>> f(n++);
>> f(n++);
>> f(n++);
>> generates inspection warning "The value changed at 'n++' is never
>> used".
>> In fact, 'n' is used: as a function parameter.
>>
>> JetBrains, could you fix this minor inconvenience? In the spare time,
>> of course :)
>>
>> TomP
>>

0
Comment actions Permalink

Hello TomP,

You may agree or not, but the inspection result is correct. :)

I'd say the right thing to do here is to use a builder patern:

StatementBuilder builder = createBuilder(connection, sql);
builder.addString(paramA).addString(paramB).addString(paramC);
builder.getStatement();

Alternatively, you can suppress the inspection.

I don't agree.
I frequently use this pattern in the following scenario:
PreparedStatement sqlStatement = connection.prepareStatement(sql);
int n = 1;
sqlStatement.setString(n++, paramA);
sqlStatement.setString(n++, paramB);
sqlStatement.setString(n++, paramC);
instead of
sqlStatement.setString(1, paramA);
sqlStatement.setString(2, paramB);
sqlStatement.setString(3, paramC);
Now, if I want to change the order of assigning parameters paramB and
paramC, then I just change the order of lines in the code to:
sqlStatement.setString(n++, paramA);
sqlStatement.setString(n++, paramC);
sqlStatement.setString(n++, paramB);
instead of error prone code:
sqlStatement.setString(1, paramA);
sqlStatement.setString(3, paramB);
sqlStatement.setString(2, paramC);
TomP

Dmitry Jemerov wrote:

>> Hello TomP,
>>
>> The value of "n" is used, but the result of the increment operation
>> is not. The meaning of the code will not change if you replace the
>> last n++ with simply n.
>>
>>> IntellijIdea 8.1.3
>>>
>>> Following code:
>>> int n=1;
>>> f(n++);
>>> f(n++);
>>> f(n++);
>>> generates inspection warning "The value changed at 'n++' is never
>>> used".
>>> In fact, 'n' is used: as a function parameter.
>>> JetBrains, could you fix this minor inconvenience? In the spare
>>> time, of course :)
>>>
>>> TomP
>>>
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Dmitry,
Agree or not, it's a minor inconvenience.
Thanks for the builder pattern hint.
While you are online, can you answer this:
"How to locally disable, and than enable this inspection?"
TomP


Dmitry Jemerov wrote:

Hello TomP,

You may agree or not, but the inspection result is correct. :)

I'd say the right thing to do here is to use a builder patern:

StatementBuilder builder = createBuilder(connection, sql);
builder.addString(paramA).addString(paramB).addString(paramC);
builder.getStatement();

Alternatively, you can suppress the inspection.

>> I don't agree.
>> I frequently use this pattern in the following scenario:
>> PreparedStatement sqlStatement = connection.prepareStatement(sql);
>> int n = 1;
>> sqlStatement.setString(n++, paramA);
>> sqlStatement.setString(n++, paramB);
>> sqlStatement.setString(n++, paramC);
>> instead of
>> sqlStatement.setString(1, paramA);
>> sqlStatement.setString(2, paramB);
>> sqlStatement.setString(3, paramC);
>> Now, if I want to change the order of assigning parameters paramB and
>> paramC, then I just change the order of lines in the code to:
>> sqlStatement.setString(n++, paramA);
>> sqlStatement.setString(n++, paramC);
>> sqlStatement.setString(n++, paramB);
>> instead of error prone code:
>> sqlStatement.setString(1, paramA);
>> sqlStatement.setString(3, paramB);
>> sqlStatement.setString(2, paramC);
>> TomP
>>
>> Dmitry Jemerov wrote:
>>
>>> Hello TomP,
>>>
>>> The value of "n" is used, but the result of the increment operation
>>> is not. The meaning of the code will not change if you replace the
>>> last n++ with simply n.
>>>
>>>> IntellijIdea 8.1.3
>>>>
>>>> Following code:
>>>> int n=1;
>>>> f(n++);
>>>> f(n++);
>>>> f(n++);
>>>> generates inspection warning "The value changed at 'n++' is never
>>>> used".
>>>> In fact, 'n' is used: as a function parameter.
>>>> JetBrains, could you fix this minor inconvenience? In the spare
>>>> time, of course :)
>>>>
>>>> TomP
>>>>

0
Comment actions Permalink

ItellijIdea is great.
I just learnt that I can disable the inspection for
statement/method/class/profile

TomP wrote:

Dmitry,
Agree or not, it's a minor inconvenience.
Thanks for the builder pattern hint.
While you are online, can you answer this:
"How to locally disable, and than enable this inspection?"
TomP


Dmitry Jemerov wrote:

>> Hello TomP,
>>
>> You may agree or not, but the inspection result is correct. :)
>>
>> I'd say the right thing to do here is to use a builder patern:
>>
>> StatementBuilder builder = createBuilder(connection, sql);
>> builder.addString(paramA).addString(paramB).addString(paramC);
>> builder.getStatement();
>>
>> Alternatively, you can suppress the inspection.
>>
>>> I don't agree.
>>> I frequently use this pattern in the following scenario:
>>> PreparedStatement sqlStatement = connection.prepareStatement(sql);
>>> int n = 1;
>>> sqlStatement.setString(n++, paramA);
>>> sqlStatement.setString(n++, paramB);
>>> sqlStatement.setString(n++, paramC);
>>> instead of
>>> sqlStatement.setString(1, paramA);
>>> sqlStatement.setString(2, paramB);
>>> sqlStatement.setString(3, paramC);
>>> Now, if I want to change the order of assigning parameters paramB and
>>> paramC, then I just change the order of lines in the code to:
>>> sqlStatement.setString(n++, paramA);
>>> sqlStatement.setString(n++, paramC);
>>> sqlStatement.setString(n++, paramB);
>>> instead of error prone code:
>>> sqlStatement.setString(1, paramA);
>>> sqlStatement.setString(3, paramB);
>>> sqlStatement.setString(2, paramC);
>>> TomP
>>>
>>> Dmitry Jemerov wrote:
>>>
>>>> Hello TomP,
>>>>
>>>> The value of "n" is used, but the result of the increment operation
>>>> is not. The meaning of the code will not change if you replace the
>>>> last n++ with simply n.
>>>>
>>>>> IntellijIdea 8.1.3
>>>>>
>>>>> Following code:
>>>>> int n=1;
>>>>> f(n++);
>>>>> f(n++);
>>>>> f(n++);
>>>>> generates inspection warning "The value changed at 'n++' is never
>>>>> used".
>>>>> In fact, 'n' is used: as a function parameter.
>>>>> JetBrains, could you fix this minor inconvenience? In the spare
>>>>> time, of course :)
>>>>>
>>>>> TomP
>>>>>

0
Comment actions Permalink

ItellijIdea is great.
I just learnt that I can disable the inspection for
statement/method/class/profile

TomP wrote:

Dmitry,
Agree or not, it's a minor inconvenience.
Thanks for the builder pattern hint.
While you are online, can you answer this:
"How to locally disable, and than enable this inspection?"
TomP


Dmitry Jemerov wrote:

>> Hello TomP,
>>
>> You may agree or not, but the inspection result is correct. :)
>>
>> I'd say the right thing to do here is to use a builder patern:
>>
>> StatementBuilder builder = createBuilder(connection, sql);
>> builder.addString(paramA).addString(paramB).addString(paramC);
>> builder.getStatement();
>>
>> Alternatively, you can suppress the inspection.
>>
>>> I don't agree.
>>> I frequently use this pattern in the following scenario:
>>> PreparedStatement sqlStatement = connection.prepareStatement(sql);
>>> int n = 1;
>>> sqlStatement.setString(n++, paramA);
>>> sqlStatement.setString(n++, paramB);
>>> sqlStatement.setString(n++, paramC);
>>> instead of
>>> sqlStatement.setString(1, paramA);
>>> sqlStatement.setString(2, paramB);
>>> sqlStatement.setString(3, paramC);
>>> Now, if I want to change the order of assigning parameters paramB and
>>> paramC, then I just change the order of lines in the code to:
>>> sqlStatement.setString(n++, paramA);
>>> sqlStatement.setString(n++, paramC);
>>> sqlStatement.setString(n++, paramB);
>>> instead of error prone code:
>>> sqlStatement.setString(1, paramA);
>>> sqlStatement.setString(3, paramB);
>>> sqlStatement.setString(2, paramC);
>>> TomP
>>>
>>> Dmitry Jemerov wrote:
>>>
>>>> Hello TomP,
>>>>
>>>> The value of "n" is used, but the result of the increment operation
>>>> is not. The meaning of the code will not change if you replace the
>>>> last n++ with simply n.
>>>>
>>>>> IntellijIdea 8.1.3
>>>>>
>>>>> Following code:
>>>>> int n=1;
>>>>> f(n++);
>>>>> f(n++);
>>>>> f(n++);
>>>>> generates inspection warning "The value changed at 'n++' is never
>>>>> used".
>>>>> In fact, 'n' is used: as a function parameter.
>>>>> JetBrains, could you fix this minor inconvenience? In the spare
>>>>> time, of course :)
>>>>>
>>>>> TomP
>>>>>

0

Please sign in to leave a comment.