Click on image to see enlargment

PC-lint/FlexeLint Output | Reference Manual Explanation | Home

            bug571.cpp

1    #include <stdio.h>
2    const char *bouquet = "DRM\xFF";
3    const char *flower( char ch )
4        {
5        switch( (unsigned) ch )
6            {
7        case 'D':  return "Daisies";
8        case 'R':  return "Roses";
9        case 'M':  return "Mums";
10       case 0xFF: return "Baby's Breath";
11       default:   return "What?";
12           }
13       }
14   int main()
15       {
16       const char *b, *flwr;
17       printf( "Bridal bouquet: \n" );
18       for( b = bouquet; *b; b++ )
19           {
20           flwr = flower( *b );
21           if(flwr) printf( "%s\n",flwr);
22           else break;
23           }
24       return 0;
25       }

A florist who knows flowers better than programming is automating his shop, but there seems to be a problem with the printout of bridal bouquets. Somehow the baby's breath is always missing. What's going on?


bug571.cpp lint Output


--- Module:   bug571.cpp (C++)
                          _
    switch( (unsigned) ch )
bug571.cpp(6) : Warning 571: Suspicious cast

Reference Manual Explanation


571    Suspicious Cast  -- Usually this warning is issued for casts of
       the form:

             (unsigned) ch

       where ch is declared as char and char is signed.  Although the
       cast may appear to prevent sign extension of ch, it does not.
       Following the normal promotion rules of C, ch is first converted
       to int which extends the sign and only then is the quantity cast
       to unsigned.  To suppress sign extension you may use:

             (unsigned char) ch

       Otherwise, if sign extension is what you want and you just want
       to suppress the warning in this instance you may use:
             (unsigned) (int) ch

       Although these examples have been given in terms of casting a
       char they will also be given whenever this cast is made upon a
       signed quantity whose size is less than the casted type.
       Examples include signed bit fields (a possibility in the new
       standard), expressions involving char, and expressions involving
       short when this type is smaller than int or a direct cast of an
       int to an unsigned long (if int is smaller than long).  This
       message is not issued for constants or for expressions involving
       bit operations.


If you have comments or questions about this bug, please post them to our   Discussion Forum


Previous Bug - Bug #794 - April 2008

Use our Interactive Demo to Run FlexeLint on our Bugs of the Month

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2009, Gimpel Software, All rights reserved.