www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Warnings about potential loss of data

reply Derek Parnell <derek psych.ward> writes:
Assuming that the idea behind the "-w" switch is to help coders locate the
assumptions that the compiler is making on our behalf, I think that it
would be useful to add an "optional error" when the compiler detects a
potential loss of data.

Example 1:
import std.stdio;
void main()
{
    uint a;
    int b;

    b = -3;
    a = b; // A warning here would be appreciated.

    writefln("a = ", a, ", b = ", b);
    // result: a = 4294967293, b = -3
}

Example 2:
void Foo()
{
    int a;
    long b;
    
    b = SomeFunction();
    a = b;  // A warning here would be appreciated.
    
}

and there are many other examples too.
-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
7/02/2006 11:16:27 AM
Feb 06 2006
next sibling parent reply Sean Kelly <sean f4.ca> writes:
Derek Parnell wrote:
 Assuming that the idea behind the "-w" switch is to help coders locate the
 assumptions that the compiler is making on our behalf, I think that it
 would be useful to add an "optional error" when the compiler detects a
 potential loss of data.
Agreed. I'd like the compiler to warn me about narrowing conversions that don't use an explicit cast. Sean
Feb 06 2006
parent "Matthew" <matthew hat.stlsoft.dot.org> writes:
"Sean Kelly" <sean f4.ca> wrote in message
news:ds8pmm$ups$1 digitaldaemon.com...
 Derek Parnell wrote:
 Assuming that the idea behind the "-w" switch is to help coders locate
the
 assumptions that the compiler is making on our behalf, I think that it
 would be useful to add an "optional error" when the compiler detects a
 potential loss of data.
Agreed. I'd like the compiler to warn me about narrowing conversions that don't use an explicit cast.
Wistfully ROFL!
Feb 06 2006
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Derek Parnell wrote:
 Assuming that the idea behind the "-w" switch is to help coders locate the
 assumptions that the compiler is making on our behalf, I think that it
 would be useful to add an "optional error" when the compiler detects a
 potential loss of data.
What is an "optional error"? Either a piece of code is legal, or it isn't, surely? <snip>
 Example 2:
 void Foo()
 {
     int a;
     long b;
     
     b = SomeFunction();
     a = b;  // A warning here would be appreciated.
     
 }
<snip> I'd like this to be an error. Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Feb 07 2006
next sibling parent U.Baumanis <U.Baumanis_member pathlink.com> writes:
In article <dsa2nv$1v8q$1 digitaldaemon.com>, Stewart Gordon says...
What is an "optional error"?  Either a piece of code is legal, or it 
isn't, surely?

<snip>
 Example 2:
 void Foo()
 {
     int a;
     long b;
     
     b = SomeFunction();
     a = b;  // A warning here would be appreciated.
     
 }
<snip> I'd like this to be an error. Stewart.
Yep this is error - some rocket can fall down because of this! -- UB
Feb 07 2006
prev sibling parent reply "Derek Parnell" <derek psych.ward> writes:
On Tue, 07 Feb 2006 23:10:39 +1100, Stewart Gordon <smjg_1998 yahoo.com>  
wrote:

 Derek Parnell wrote:
 Assuming that the idea behind the "-w" switch is to help coders locate  
 the
 assumptions that the compiler is making on our behalf, I think that it
 would be useful to add an "optional error" when the compiler detects a
 potential loss of data.
What is an "optional error"? Either a piece of code is legal, or it isn't, surely?
It was from something that Walter said about "-w" warnings ... 'Think of them as "optional errors" instead of warnings <g>.' - Walter -- Derek Parnell Melbourne, Australia
Feb 07 2006
parent Don Clugston <dac nospam.com.au> writes:
Derek Parnell wrote:
 On Tue, 07 Feb 2006 23:10:39 +1100, Stewart Gordon 
 <smjg_1998 yahoo.com>  wrote:
 
 Derek Parnell wrote:

 Assuming that the idea behind the "-w" switch is to help coders 
 locate  the
 assumptions that the compiler is making on our behalf, I think that it
 would be useful to add an "optional error" when the compiler detects a
 potential loss of data.
What is an "optional error"? Either a piece of code is legal, or it isn't, surely?
It was from something that Walter said about "-w" warnings ... 'Think of them as "optional errors" instead of warnings <g>.' - Walter
"pedantic errors" might be a better term. I think that what you're doing is telling the compiler something about your coding style (and perhaps, what kinds of bugs you create).
Feb 07 2006