www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3882] New: Unused result of pure functions

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3882

           Summary: Unused result of pure functions
           Product: D
           Version: 2.040
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



The following small program:

void main() {
    int x = 5;
    x + 3;
}

Currently produces the compile error:
Error: + has no effect in expression (x + 3)
because that's often a small bug in the program.

Equally, a pure function has no side effects, so its only purpose is to return
a value. So the compiler can issue a warning or error if its return value is
not used. This can help avoid some small bugs.

This predecessor() function computes the predecessor of its input argument, its
result is always nonnegative:


pure int predecessor(int x) {
    if (x <= 0)
        throw new Exception("");
    return x - 1;
}
void main() {
    predecessor(5); // warning or error
}


In theory a pure function can be used just for the exceptions it throws, but I
think this is a quite uncommon usage.

A "pure nothrow" function can't even have this uncommon purpose, so this can
produce a compile error:

pure nothrow int predecessor(int x) {
    return x - 1;
}
void main() {
    predecessor(5); // error
}

--------------

In GCC there is a function attribute named "warn_unused_result":
http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

The warn_unused_result attribute causes a warning to be emitted if a caller of
the function with this attribute does not use its return value. This is useful
for functions where not checking the result is either a security problem or
always a bug, such as realloc.

Similar attribute can be named like  do_use_result in D2.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 05 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3882




See also bug 5464 , that contains and expands just the second part of this
enhancement request (because putting two different requests in a bugzilla issue
is bad).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 28 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3882




If the druntime function to allocate a new array is a pure function, then this
enhancement request catches a bug like this too:

{
    new int[10];
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 27 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3882




KennyTM~ reminds that this is error message is only for "strongly pure"
functions.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 27 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3882




Walter seems to appreciate the idea:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=132932

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 27 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3882


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED



20:44:39 PDT ---
https://github.com/D-Programming-Language/dmd/commit/9b50184ca4e66a4b878468b7eb0cb21839684c81

I added this as a warning because it's fairly onerous now that we have implicit
purity deduction.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 25 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3882






 I added this as a warning because it's fairly onerous now that we have implicit
 purity deduction.
Thank you very much Walter. DMD 2.054 is becoming an interesting release. In few months the warning will allow us to see _how much_ onerous a similar error is. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 25 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3882


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|FIXED                       |WONTFIX



I change this to WONTFIX because practice has already shown this change is not
currently practical. More thinking is needed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 27 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3882


Trass3r <mrmocool gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mrmocool gmx.de



*** Issue 7235 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 05 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3882


Trass3r <mrmocool gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|WONTFIX                     |LATER


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 05 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3882


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |per.nordlow gmail.com



*** Issue 9632 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 02 2013