danielmarjamaki updated this revision to Diff 92322.
danielmarjamaki added a comment.
Remove warnings for implicit casts.
Repository:
rL LLVM
https://reviews.llvm.org/D31097
Files:
clang-tidy/misc/MisplacedWideningCastCheck.cpp
clang-tidy/misc/MisplacedWideningCastCheck.h
docs/clang-tidy/checks/misc-misplaced-widening-cast.rst
test/clang-tidy/misc-misplaced-widening-cast-explicit-only.cpp
test/clang-tidy/misc-misplaced-widening-cast.cpp
Index: test/clang-tidy/misc-misplaced-widening-cast.cpp
===================================================================
--- test/clang-tidy/misc-misplaced-widening-cast.cpp
+++ test/clang-tidy/misc-misplaced-widening-cast.cpp
@@ -6,13 +6,11 @@
long l;
l = a * b;
- // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long' is ineffective, or there is loss of precision before the conversion [misc-misplaced-widening-cast]
l = (long)(a * b);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
l = (long)a * b;
l = a << 8;
- // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
l = (long)(a << 8);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
l = (long)b << 8;
@@ -25,9 +23,7 @@
bool l;
l = a * b == c;
- // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
l = c == a * b;
- // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: either cast from 'int' to 'long'
l = (long)(a * b) == c;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
l = c == (long)(a * b);
@@ -38,24 +34,21 @@
void init(unsigned int n) {
long l1 = n << 8;
- // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: either cast from 'unsigned int' to 'long'
long l2 = (long)(n << 8);
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: either cast from 'unsigned int' to 'long'
long l3 = (long)n << 8;
}
void call(unsigned int n) {
func(n << 8);
- // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: either cast from 'unsigned int' to 'long'
func((long)(n << 8));
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: either cast from 'unsigned int' to 'long'
func((long)n << 8);
}
long ret(int a) {
if (a < 0) {
return a * 1000;
- // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: either cast from 'int' to 'long'
} else if (a > 0) {
return (long)(a * 1000);
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: either cast from 'int' to 'long'
Index: test/clang-tidy/misc-misplaced-widening-cast-explicit-only.cpp
===================================================================
--- test/clang-tidy/misc-misplaced-widening-cast-explicit-only.cpp
+++ test/clang-tidy/misc-misplaced-widening-cast-explicit-only.cpp
@@ -1,58 +0,0 @@
-// RUN: %check_clang_tidy %s misc-misplaced-widening-cast %t -- -config="{CheckOptions: [{key: misc-misplaced-widening-cast.CheckImplicitCasts, value: 0}]}" --
-
-void func(long arg) {}
-
-void assign(int a, int b) {
- long l;
-
- l = a * b;
- l = (long)(a * b);
- // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long' is ineffective, or there is loss of precision before the conversion [misc-misplaced-widening-cast]
- l = (long)a * b;
-
- l = a << 8;
- l = (long)(a << 8);
- // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
- l = (long)b << 8;
-
- l = static_cast<long>(a * b);
- // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
-}
-
-void compare(int a, int b, long c) {
- bool l;
-
- l = a * b == c;
- l = c == a * b;
- l = (long)(a * b) == c;
- // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
- l = c == (long)(a * b);
- // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: either cast from 'int' to 'long'
- l = (long)a * b == c;
- l = c == (long)a * b;
-}
-
-void init(unsigned int n) {
- long l1 = n << 8;
- long l2 = (long)(n << 8);
- // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: either cast from 'unsigned int' to 'long'
- long l3 = (long)n << 8;
-}
-
-void call(unsigned int n) {
- func(n << 8);
- func((long)(n << 8));
- // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: either cast from 'unsigned int' to 'long'
- func((long)n << 8);
-}
-
-long ret(int a) {
- if (a < 0) {
- return a * 1000;
- } else if (a > 0) {
- return (long)(a * 1000);
- // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: either cast from 'int' to 'long'
- } else {
- return (long)a * 1000;
- }
-}
Index: docs/clang-tidy/checks/misc-misplaced-widening-cast.rst
===================================================================
--- docs/clang-tidy/checks/misc-misplaced-widening-cast.rst
+++ docs/clang-tidy/checks/misc-misplaced-widening-cast.rst
@@ -32,18 +32,7 @@
return (long)x * 1000;
}
-Implicit casts
---------------
-
-Forgetting to place the cast at all is at least as dangerous and at least as
-common as misplacing it. If :option:`CheckImplicitCasts` is enabled the check
-also detects these cases, for instance:
-.. code-block:: c++
-
- long f(int x) {
- return x * 1000;
- }
Floating point
--------------
@@ -56,10 +45,3 @@
double f(float x) {
return (double)(x * 10.0f);
}
-
-Options
--------
-
-.. option:: CheckImplicitCasts
-
- If non-zero, enables detection of implicit casts. Default is non-zero.
Index: clang-tidy/misc/MisplacedWideningCastCheck.h
===================================================================
--- clang-tidy/misc/MisplacedWideningCastCheck.h
+++ clang-tidy/misc/MisplacedWideningCastCheck.h
@@ -21,22 +21,13 @@
/// the cast is misplaced, and there can be loss of precision. Otherwise
/// such cast is ineffective.
///
-/// There is one option:
-///
-/// - `CheckImplicitCasts`: Whether to check implicit casts as well which may
-// be the most common case. Enabled by default.
-///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/misc-misplaced-widening-cast.html
class MisplacedWideningCastCheck : public ClangTidyCheck {
public:
MisplacedWideningCastCheck(StringRef Name, ClangTidyContext *Context);
- void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
-
-private:
- const bool CheckImplicitCasts;
};
} // namespace misc
Index: clang-tidy/misc/MisplacedWideningCastCheck.cpp
===================================================================
--- clang-tidy/misc/MisplacedWideningCastCheck.cpp
+++ clang-tidy/misc/MisplacedWideningCastCheck.cpp
@@ -20,13 +20,7 @@
MisplacedWideningCastCheck::MisplacedWideningCastCheck(
StringRef Name, ClangTidyContext *Context)
- : ClangTidyCheck(Name, Context),
- CheckImplicitCasts(Options.get("CheckImplicitCasts", true)) {}
-
-void MisplacedWideningCastCheck::storeOptions(
- ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "CheckImplicitCasts", CheckImplicitCasts);
-}
+ : ClangTidyCheck(Name, Context) {}
void MisplacedWideningCastCheck::registerMatchers(MatchFinder *Finder) {
const auto Calc =
@@ -37,12 +31,9 @@
hasType(isInteger()))
.bind("Calc");
- const auto ExplicitCast = explicitCastExpr(hasDestinationType(isInteger()),
- has(ignoringParenImpCasts(Calc)));
- const auto ImplicitCast =
- implicitCastExpr(hasImplicitDestinationType(isInteger()),
- has(ignoringParenImpCasts(Calc)));
- const auto Cast = expr(anyOf(ExplicitCast, ImplicitCast)).bind("Cast");
+ const auto Cast = explicitCastExpr(hasDestinationType(isInteger()),
+ has(ignoringParenImpCasts(Calc)))
+ .bind("Cast");
Finder->addMatcher(varDecl(hasInitializer(Cast)), this);
Finder->addMatcher(returnStmt(hasReturnValue(Cast)), this);
@@ -183,8 +174,6 @@
void MisplacedWideningCastCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Cast = Result.Nodes.getNodeAs<CastExpr>("Cast");
- if (!CheckImplicitCasts && isa<ImplicitCastExpr>(Cast))
- return;
if (Cast->getLocStart().isMacroID())
return;
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits