felix642 created this revision. Herald added subscribers: PiotrZSL, xazax.hun. Herald added a reviewer: njames93. Herald added a project: All. felix642 requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits.
If a parameter value is either 'none', 'null', 'false', '-1' or '', we will in that case use the default value Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D159436 Files: clang-tools-extra/clang-tidy/ClangTidyCheck.h clang-tools-extra/test/clang-tidy/infrastructure/optional-parameter.cpp Index: clang-tools-extra/test/clang-tidy/infrastructure/optional-parameter.cpp =================================================================== --- /dev/null +++ clang-tools-extra/test/clang-tidy/infrastructure/optional-parameter.cpp @@ -0,0 +1,24 @@ +// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \ +// RUN: -config='{CheckOptions: { \ +// RUN: bugprone-easily-swappable-parameters.MinimumLength: "", \ +// RUN: }}' -- + +// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \ +// RUN: -config='{CheckOptions: { \ +// RUN: bugprone-easily-swappable-parameters.MinimumLength: "none", \ +// RUN: }}' -- + +// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \ +// RUN: -config='{CheckOptions: { \ +// RUN: bugprone-easily-swappable-parameters.MinimumLength: "null", \ +// RUN: }}' -- + +// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \ +// RUN: -config='{CheckOptions: { \ +// RUN: bugprone-easily-swappable-parameters.MinimumLength: "false", \ +// RUN: }}' -- + +void a(int b, int c) {} +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: 2 adjacent parameters of 'a' of similar type ('int') are easily swapped by mistake [bugprone-easily-swappable-parameters] +// CHECK-MESSAGES: :[[@LINE-2]]:12: note: the first parameter in the range is 'b' +// CHECK-MESSAGES: :[[@LINE-3]]:19: note: the last parameter in the range is 'c' Index: clang-tools-extra/clang-tidy/ClangTidyCheck.h =================================================================== --- clang-tools-extra/clang-tidy/ClangTidyCheck.h +++ clang-tools-extra/clang-tidy/ClangTidyCheck.h @@ -184,8 +184,8 @@ /// integral type ``T``. /// /// Reads the option with the check-local name \p LocalName from the - /// ``CheckOptions``. If the corresponding key is not present, return - /// ``std::nullopt``. + /// ``CheckOptions``. If the corresponding key is not present or empty, + /// return ``std::nullopt``. /// /// If the corresponding key can't be parsed as a ``T``, emit a /// diagnostic and return ``std::nullopt``. @@ -193,6 +193,9 @@ std::enable_if_t<std::is_integral_v<T>, std::optional<T>> get(StringRef LocalName) const { if (std::optional<StringRef> Value = get(LocalName)) { + if (Value == "" || Value == "none" || Value == "null" || + Value == "false" || (std::is_unsigned_v<T> && Value == "-1")) + return std::nullopt; T Result{}; if (!StringRef(*Value).getAsInteger(10, Result)) return Result; @@ -286,8 +289,8 @@ /// enum type ``T``. /// /// Reads the option with the check-local name \p LocalName from the - /// ``CheckOptions``. If the corresponding key is not present, return - /// \p Default. + /// ``CheckOptions``. If the corresponding key is not present or empty, + /// return \p Default. /// /// If the corresponding key can't be parsed as a ``T``, emit a /// diagnostic and return \p Default.
Index: clang-tools-extra/test/clang-tidy/infrastructure/optional-parameter.cpp =================================================================== --- /dev/null +++ clang-tools-extra/test/clang-tidy/infrastructure/optional-parameter.cpp @@ -0,0 +1,24 @@ +// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \ +// RUN: -config='{CheckOptions: { \ +// RUN: bugprone-easily-swappable-parameters.MinimumLength: "", \ +// RUN: }}' -- + +// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \ +// RUN: -config='{CheckOptions: { \ +// RUN: bugprone-easily-swappable-parameters.MinimumLength: "none", \ +// RUN: }}' -- + +// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \ +// RUN: -config='{CheckOptions: { \ +// RUN: bugprone-easily-swappable-parameters.MinimumLength: "null", \ +// RUN: }}' -- + +// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \ +// RUN: -config='{CheckOptions: { \ +// RUN: bugprone-easily-swappable-parameters.MinimumLength: "false", \ +// RUN: }}' -- + +void a(int b, int c) {} +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: 2 adjacent parameters of 'a' of similar type ('int') are easily swapped by mistake [bugprone-easily-swappable-parameters] +// CHECK-MESSAGES: :[[@LINE-2]]:12: note: the first parameter in the range is 'b' +// CHECK-MESSAGES: :[[@LINE-3]]:19: note: the last parameter in the range is 'c' Index: clang-tools-extra/clang-tidy/ClangTidyCheck.h =================================================================== --- clang-tools-extra/clang-tidy/ClangTidyCheck.h +++ clang-tools-extra/clang-tidy/ClangTidyCheck.h @@ -184,8 +184,8 @@ /// integral type ``T``. /// /// Reads the option with the check-local name \p LocalName from the - /// ``CheckOptions``. If the corresponding key is not present, return - /// ``std::nullopt``. + /// ``CheckOptions``. If the corresponding key is not present or empty, + /// return ``std::nullopt``. /// /// If the corresponding key can't be parsed as a ``T``, emit a /// diagnostic and return ``std::nullopt``. @@ -193,6 +193,9 @@ std::enable_if_t<std::is_integral_v<T>, std::optional<T>> get(StringRef LocalName) const { if (std::optional<StringRef> Value = get(LocalName)) { + if (Value == "" || Value == "none" || Value == "null" || + Value == "false" || (std::is_unsigned_v<T> && Value == "-1")) + return std::nullopt; T Result{}; if (!StringRef(*Value).getAsInteger(10, Result)) return Result; @@ -286,8 +289,8 @@ /// enum type ``T``. /// /// Reads the option with the check-local name \p LocalName from the - /// ``CheckOptions``. If the corresponding key is not present, return - /// \p Default. + /// ``CheckOptions``. If the corresponding key is not present or empty, + /// return \p Default. /// /// If the corresponding key can't be parsed as a ``T``, emit a /// diagnostic and return \p Default.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits