Merge: typing: new warning `useless-truism` to catch trainees.
authorJean Privat <jean@pryen.org>
Tue, 18 Jul 2017 12:38:12 +0000 (08:38 -0400)
committerJean Privat <jean@pryen.org>
Tue, 18 Jul 2017 12:38:12 +0000 (08:38 -0400)
Warning on `if a == true then` constructions.

Pull-Request: #2520
Reviewed-by: Jean-Christophe Beaupré <jcbrinfo.public@gmail.com>

15 files changed:
contrib/benitlux/src/server/benitlux_daily.nit
lib/curl/native_curl.nit
lib/gamnit/network/client.nit
lib/nitcorn/examples/src/htcpcp_server.nit
src/interpreter/naive_interpreter.nit
src/metrics/codesmells_metrics.nit
src/metrics/detect_covariance.nit
src/metrics/method_analyze_metrics.nit
src/model/model.nit
src/semantize/typing.nit
src/vm/virtual_machine.nit
tests/sav/warn_truism.res [new file with mode: 0644]
tests/test_postgres_native.nit
tests/test_postgres_nity.nit
tests/warn_truism.nit [new file with mode: 0644]

index 7878de2..4268c8e 100644 (file)
@@ -246,7 +246,7 @@ end
 if "NIT_TESTING".environ == "true" then exit 0
 
 opts.parse args
-if not opts.errors.is_empty or opts.help.value == true then
+if not opts.errors.is_empty or opts.help.value then
        print opts.errors.join("\n")
        print "Usage: benitlux_daily [Options]"
        opts.usage
index d611dbf..1f5cd11 100644 (file)
@@ -87,8 +87,8 @@ extern class NativeCurl `{ CURL * `}
        fun easy_setopt(opt: CURLOption, obj: Object): CURLCode
        do
                if obj isa Int then return native_setopt_int(opt, obj)
-               if obj isa Bool and obj == true then return native_setopt_int(opt, 1)
-               if obj isa Bool and obj == false then return native_setopt_int(opt, 0)
+               if obj == true then return native_setopt_int(opt, 1)
+               if obj == false then return native_setopt_int(opt, 0)
                if obj isa String then return native_setopt_string(opt, obj)
                if obj isa FileWriter then return native_setopt_file(opt, obj._file.as(not null))
                if obj isa CURLSList then return native_setopt_slist(opt, obj)
@@ -324,7 +324,7 @@ extern class CURLSList `{ struct curl_slist * `}
                var r = new Array[String]
                var cursor = self
                loop
-                       if native_data_reachable(cursor) != true then break
+                       if not native_data_reachable(cursor) then break
                        r.add(native_data(cursor))
                        cursor = native_next(cursor)
                end
index 8aad96f..95afff8 100644 (file)
@@ -62,7 +62,7 @@ class RemoteServer
        var socket: nullable TCPStream = null
 
        # Is this connection connected?
-       fun connected: Bool do return socket != null and socket.connected == true
+       fun connected: Bool do return socket != null and socket.connected
 
        # `BinarySerializer` used to send data to this client through `socket`
        var writer: BinarySerializer is noinit
index 315d76f..73177a1 100644 (file)
@@ -36,7 +36,7 @@ class HTCPCPAction
                var method = http_request.method
                var response: HttpResponse
 
-               if is_teapot == true then
+               if is_teapot then
                        response = new HttpResponse(418)
                        response.body = "I'm a teapot!\n"
                        response.header["Content-Type"] = "text"
index 991d8b5..616977a 100644 (file)
@@ -839,7 +839,7 @@ redef class ANode
                        abort
                end
 
-               if v.modelbuilder.toolcontext.opt_no_color.value == true then
+               if v.modelbuilder.toolcontext.opt_no_color.value then
                        sys.stderr.write("Runtime error: {message} ({location.file.filename}:{location.line_start})\n")
                else
                        sys.stderr.write("{location}: Runtime error: {message}\n{location.colored_line("0;31")}\n")
index 1bd502d..d09e769 100644 (file)
@@ -117,7 +117,7 @@ class BadConceptionFinder
                bad_conception_elements.add(new FeatureEnvy(phase))
                bad_conception_elements.add(new LongMethod(phase))
                for bad_conception_element in bad_conception_elements do
-                       if bad_conception_element.collect(self.mclassdef,phase.toolcontext.modelbuilder) == true then array_badconception.add(bad_conception_element)
+                       if bad_conception_element.collect(self.mclassdef,phase.toolcontext.modelbuilder) then array_badconception.add(bad_conception_element)
                end
        end
 
index 643679a..ba6a684 100644 (file)
@@ -494,7 +494,7 @@ redef class MType
                if anchor == null then anchor = sub # UGLY: any anchor will work
                var resolved_sub = sub.anchor_to(mmodule, anchor)
                var res = resolved_sub.collect_mclasses(mmodule).has(sup.mclass)
-               if res == false then return false
+               if not res then return false
                if not sup isa MGenericType then return true
                var sub2 = sub.supertype_to(mmodule, anchor, sup.mclass)
                assert sub2.mclass == sup.mclass
@@ -502,10 +502,10 @@ redef class MType
                        var sub_arg = sub2.arguments[i]
                        var sup_arg = sup.arguments[i]
                        res = sub_arg.is_subtype(mmodule, anchor, sup_arg)
-                       if res == false then return false
+                       if not res then return false
                        # The two new lines
                        res = sup_arg.is_subtype(mmodule, anchor, sub_arg)
-                       if res == false then return false
+                       if not res then return false
                        # End of the two new lines
                end
                return true
index 6aee2a9..7adbd49 100644 (file)
@@ -69,7 +69,7 @@ public class MethodAnalyzeMetrics
                        var callsite = n.callsite
                        if callsite != null then
                                self.total_call += 1
-                               if callsite.recv_is_self == true then self.total_self_call += 1
+                               if callsite.recv_is_self then self.total_self_call += 1
                        end
                end
        end
index 5d92dfd..935b201 100644 (file)
@@ -885,7 +885,7 @@ abstract class MType
                if anchor == null then anchor = sub # UGLY: any anchor will work
                var resolved_sub = sub.anchor_to(mmodule, anchor)
                var res = resolved_sub.collect_mclasses(mmodule).has(sup.mclass)
-               if res == false then return false
+               if not res then return false
                if not sup isa MGenericType then return true
                var sub2 = sub.supertype_to(mmodule, anchor, sup.mclass)
                assert sub2.mclass == sup.mclass
@@ -893,7 +893,7 @@ abstract class MType
                        var sub_arg = sub2.arguments[i]
                        var sup_arg = sup.arguments[i]
                        res = sub_arg.is_subtype(mmodule, anchor, sup_arg)
-                       if res == false then return false
+                       if not res then return false
                end
                return true
        end
index 8890ba9..3297d12 100644 (file)
@@ -2017,6 +2017,10 @@ redef class AEqFormExpr
 
                if mtype == null or mtype2 == null then return
 
+               if mtype == v.type_bool(self) and (n_expr2 isa AFalseExpr or n_expr2 isa ATrueExpr) then
+                       v.modelbuilder.warning(self, "useless-truism", "Warning: useless comparison to a Bool literal.")
+               end
+
                if not mtype2 isa MNullType then return
 
                v.check_can_be_null(n_expr, mtype)
index 07ad951..c7e57c6 100644 (file)
@@ -116,7 +116,7 @@ class VirtualMachine super NaiveInterpreter
                var mask = sub.mclass.vtable.mask
 
                var res = inter_is_subtype_ph(super_id, mask, sub.mclass.vtable.internal_vtable)
-               if res == false then return false
+               if not res then return false
                # sub and sup can be generic types, each argument of generics has to be tested
 
                if not sup isa MGenericType then return true
@@ -127,7 +127,7 @@ class VirtualMachine super NaiveInterpreter
                        var sub_arg = sub2.arguments[i]
                        var sup_arg = sup.arguments[i]
                        var res2 = is_subtype(sub_arg, sup_arg)
-                       if res2 == false then return false
+                       if not res2 then return false
                end
                return true
        end
diff --git a/tests/sav/warn_truism.res b/tests/sav/warn_truism.res
new file mode 100644 (file)
index 0000000..7b485c1
--- /dev/null
@@ -0,0 +1,4 @@
+warn_truism.nit:18,4--12: Warning: useless comparison to a Bool literal.
+warn_truism.nit:19,9--18: Warning: useless comparison to a Bool literal.
+warn_truism.nit:20,10--26: Warning: useless comparison to a Bool literal.
+warn_truism.nit:20,33--41: Warning: useless comparison to a Bool literal.
index a228993..b7c5a49 100644 (file)
@@ -37,7 +37,7 @@ assert postgres_select: result.status.is_ok else print_error db.error
 assert postgres_ntuples: result.ntuples == 2 else print_error db.error
 assert postgres_nfields: result.nfields == 3 else print_error db.error
 assert postgres_fname: result.fname(0) == "aname" else print_error db.error
-assert postgres_isnull: result.is_null(0,0) == false else print_error db.error
+assert postgres_isnull: not result.is_null(0,0) else print_error db.error
 assert postgres_value: result.value(0,0) == "Whale" else print_error db.error
 
 var cols: Int = result.nfields
index 900aac9..4421a02 100644 (file)
@@ -40,7 +40,7 @@ assert raw_exec: result.is_ok else print db.error
 
 assert postgres_nfields: result.nfields == 4 else print_error db.error
 assert postgres_fname: result.fname(0) == "uname" else print_error db.error
-assert postgres_isnull: result.is_null(0,0) == false else print_error db.error
+assert postgres_isnull: not result.is_null(0,0) else print_error db.error
 assert postgres_value: result.value(0,0) == "Bob" else print_error db.error
 
 assert drop_table: db.execute("DROP TABLE users_{db_suffix}") else print db.error
diff --git a/tests/warn_truism.nit b/tests/warn_truism.nit
new file mode 100644 (file)
index 0000000..32e1e1d
--- /dev/null
@@ -0,0 +1,29 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import core::kernel
+
+var a = true
+if a == true then end
+var b = a != false
+var c = ((a or b) == false) and b != true
+
+# only warn the common case (not those)
+if true == a then end
+if a == (true) then end
+
+# dont warn if operand 1 is not a bool
+var o: nullable Bool = a
+if o == true then end
+if o or else false then end