This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Warn on redundant if statement


A warning is added for if statements which have no else or elsif,
and a then that is a single null statement, and a condition that
has no obvious side effects. The following is compiled with
-gnatwr.d

     1. function WarnOnIf (X : Integer) return Boolean is
     2.    V : Integer with Volatile;
     3.
     4. begin
     5.    V := 32;
     6.
     7.    if True then                     -- warn
           |
        >>> warning: if statement has no effect [-gnatwr]

     8.       null;
     9.    end if;
    10.
    11.    if X > 5 or else X < 3 then      -- warn
           |
        >>> warning: if statement has no effect [-gnatwr]

    12.       null;
    13.    end if;
    14.
    15.    if WarnOnIf (X - 1) then         -- no warn (call)
    16.       null;
    17.    end if;
    18.
    19.    if V > 3 then                    -- no warn (volatile)
    20.       null;
    21.    end if;
    22.
    23.    return False;
    24. end WarnOnIf;

Tested on x86_64-pc-linux-gnu, committed on trunk

2013-10-10  Robert Dewar  <dewar@adacore.com>

	* sem_ch5.adb (Analyze_If_Statement): Warn on redundant if
	statement.
	* sem_util.ads, sem_util.adb (Has_No_Obvious_Side_Effects): New
	function.

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]