inspector bugs.

Inspector error in 94.633,

The inspector is giving a lot of bad advice.

For example:

private int toPixelY( double y )
        {
        return yPixelBase - (int) Math.round( ( y - ylow ) * scaley );
        }

It thinks you can remove the (int) cast.

yPixelBase, y, ylow are int. scaley is double.


Similarly it thinks you can remove the cast in:

  final int estimatedLines = ( int ) file.length() / 50;

where file is a File.


Any code of this form:

int v = -1 /* default value */;
try {
     v = Integer.parseInt( s );
     }
catch ( InvalidNumberFormatExecption e )
{
}

out.println( v );

It thinks you can get rid of the initial value.  When you take it out,
of course JavaC complains.
--
Roedy Green Canadian Mind Products
http://mindprod.com

If you tell a computer the same fact in more than one place, unless you have an automated mechanism to ensure           they stay in sync, the versions of the fact will eventually get out of sync.

5 comments

well unless I'm mistaken

final int estimatedLines = ( int ) file.length() / 50;

the cast is indeed superfluous as java will automatically cast the result of file.length() / 50 to int already  as this is the type of your variable

I assume the same can be said about your other cases

0

Roedy, I was unable to reproduce any of these issues.
Could you please submit an issue into the http://youtrack.jetbrains.net/
with complete examples so that we can reproduce them?

"Roedy Green" <see_website@mindprod.com.invalid> wrote in message
news:8l85r5hr7akm973sdsi0gitnqus4g1s2np@4ax.com...

Inspector error in 94.633,

>

The inspector is giving a lot of bad advice.

>

For example:

>

private int toPixelY( double y )
       {
       return yPixelBase - (int) Math.round( ( y - ylow ) * scaley );
       }

>

It thinks you can remove the (int) cast.

>

yPixelBase, y, ylow are int. scaley is double.

>
>

Similarly it thinks you can remove the cast in:

>

final int estimatedLines = ( int ) file.length() / 50;

>

where file is a File.

>
>

Any code of this form:

>

int v = -1 /* default value */;
try {
    v = Integer.parseInt( s );
    }
catch ( InvalidNumberFormatExecption e )
{
}

>

out.println( v );

>

It thinks you can get rid of the initial value.  When you take it out,
of course JavaC complains.
--
Roedy Green Canadian Mind Products
http://mindprod.com

>

If you tell a computer the same fact in more than one place, unless you
have an automated mechanism to ensure           they stay in sync, the
versions of the fact will eventually get out of sync.


0

On Wed, 31 Mar 2010 09:00:44 MSD, Thibaut   <no_reply@jetbrains.com>
wrote, quoted or indirectly quoted someone who said :

>well unless I'm mistaken
>
>final int estimatedLines = ( int ) file.length() / 50;
>
>the cast is indeed superfluous as java will automatically cast the result of file.length() / 50 to int already  as this is the type of your variable
>
>I assume the same can be said about your other cases

There is no automatic cast of long to int. There is int to long and
long to Long, but not long to int.

Prove it to yourself with this SSCCE.  Try taking out the cast and see
if it compiles.


/*

  • @(#)InspectorCast.java

*

  • Summary: Demonstrate problem with IntelliJ inspector recommending

removing casts it should not.
*

  • Copyright: (c) 2008-2010 Roedy Green, Canadian Mind Products,

http://mindprod.com
*

  • Licence: This software may be copied and used freely for any

purpose but military.

*

  • Requires: JDK 1.6+

*

  • Created with: IntelliJ IDEA IDE.

*

  • Version History:

  • 1.0 2010-03-31 - initial version

*/
package com.mindprod.sscce;

import static java.lang.System.out;

/**

  • Demonstrate problem with IntelliJ inspector recommending removing

casts it should not.

  • <p/>

*

  • @author Roedy Green, Canadian Mind Products

  • @version 1.0 2010-03-31 - initial version

  • @since 2010-03-31

*/

public class InspectorCast
    {
    public static void main( String[] args )
        {
        // Math.round returns a long.
        final int x = ( int ) Math.round( 12345.457D );
        // inspector does not erroneously suggest removing cast in
this context.
        out.println( x );
        }
    }
--
Roedy Green Canadian Mind Products
http://mindprod.com

If you tell a computer the same fact in more than one place, unless you have an automated mechanism to ensure           they stay in sync, the versions of the fact will eventually get out of sync.

0

Roedy,

I also do not see any problems with the inspections. (I'm using IU-95.4)  As Thibeaut said, the one example is correct, and the other examples IDEA does not recommrend you remove the cast.  I turned on all inspections, and the only warning I got on the others was "Cast from 'int' to 'long' may result in a loss of precision" with no quickfix offered.

Did you see these after getting some exceptions perhaps?

-Alex
roedy-example2.png
roedy-example.png

0

On Thu, 01 Apr 2010 08:12:29 MSD, Alex   <no_reply@jetbrains.com>
wrote, quoted or indirectly quoted someone who said :

>
>I also do not see any problems with the inspections. (I'm using IU-95.4)  As Thibeaut said, the one example is correct, and the other examples IDEA does not recommrend you remove the cast.  I turned on all inspections, and the only warning I got on the others was "Cast from 'int' to 'long' may result in a loss of precision" with no quickfix offered.
>
>Did you see these after getting some exceptions perhaps?

There is something peculiar going on.  The inspector does not make
making these recommendations when I ask on a file by file basis.  The
time it did, was when I asked it to handle the entire project in a
lump.  That was 1088 classes and 6 modules.

I will do more experiments to see if I can nail down what is
happening.
--
Roedy Green Canadian Mind Products
http://mindprod.com

If you tell a computer the same fact in more than one place, unless you have an automated mechanism to ensure           they stay in sync, the versions of the fact will eventually get out of sync.

0

Please sign in to leave a comment.