Skip to content

Commit

Permalink
Add explicit to conversions (pytorch#9336)
Browse files Browse the repository at this point in the history
Summary:
Another code-mod for clang-tidy: Conversion operators should be marked explicit so that they don't cause unwanted implicit conversions. This is especially important for `operator bool()`, see https://stackoverflow.com/questions/39995573/when-can-i-use-explicit-operator-bool-without-a-cast

ezyang apaszke
Pull Request resolved: pytorch#9336

Reviewed By: apaszke

Differential Revision: D8807065

Pulled By: goldsborough

fbshipit-source-id: 0e9f4ebd0048a2a510c0d05fa410695d7e977eb1
  • Loading branch information
goldsborough authored and facebook-github-bot committed Jul 11, 2018
1 parent c2dd90c commit 18a9752
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
44 changes: 23 additions & 21 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,38 @@
Checks: '
*
,modernize-*
,clang-analyzer-*
,-cert-err58-cpp
,-cert-err60-cpp
,-clang-diagnostic-*
,-hicpp-no-array-decay
,-cppcoreguidelines-owning-memory
,-cppcoreguidelines-pro-bounds-array-to-pointer-decay
,-cppcoreguidelines-pro-bounds-constant-array-index
,-cppcoreguidelines-pro-type-static-cast-downcast
,-cppcoreguidelines-pro-type-vararg
,-cppcoreguidelines-special-member-functions
,-fuchsia-*
,-google-build-using-namespace
,-google-explicit-constructor
,-google-readability-braces-around-statements
,-google-readability-namespace-comments
,-llvm-namespace-comment
,-google-readability-todo
,-cppcoreguidelines-pro-bounds-array-to-pointer-decay
,-cert-err60-cpp
,-llvm-header-guard
,-cppcoreguidelines-special-member-functions
,-misc-unused-parameters
,-google-runtime-references
,-google-runtime-references
,-hicpp-braces-around-statements
,-hicpp-explicit-conversions
,-hicpp-no-array-decay
,-hicpp-special-member-functions
,-readability-braces-around-statements
,-modernize-use-default-member-init
,-google-runtime-references
,-cppcoreguidelines-pro-type-vararg
,-google-readability-braces-around-statements
,-google-build-using-namespace
,-hicpp-vararg
,-hicpp-explicit-conversions
,-performance-unnecessary-value-param
,-google-runtime-references
,-cppcoreguidelines-pro-type-static-cast-downcast
,-cppcoreguidelines-pro-bounds-constant-array-index
,-cert-err58-cpp
,-llvm-header-guard
,-llvm-namespace-comment
,-misc-unused-parameters
,-modernize-make-unique
,-cppcoreguidelines-owning-memory
,-modernize-use-default-member-init
,-performance-unnecessary-value-param
,-readability-braces-around-statements
,-readability-else-after-return
,-readability-named-parameter
,clang-analyzer-*
'
WarningsAsErrors: ''
HeaderFilterRegex: 'torch/csrc/'
Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/jit/autodiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ using value_list = std::vector<Value*>;
// Terminology: vjp = vector-jacobian product

struct Gradient {
operator bool() const {
explicit operator bool() const {
return df != nullptr;
}
std::shared_ptr<Graph> f;
Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/jit/graph_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct GraphExecutor {
// note: if not specified, symbolically_differentiable is computed from the graph.
GraphExecutor(std::shared_ptr<Graph> graph, bool optimize, bool symbolically_differentiable);
variable_tensor_list run(variable_tensor_list && inputs);
operator bool() const {
explicit operator bool() const {
return pImpl != nullptr;
}
std::shared_ptr<Graph> graph() const;
Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/jit/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct Code {
// Returns pointers to GraphExecutors created to run GraphExecutor nodes in the given graph.
const std::vector<GraphExecutor*>& executors();

operator bool() const {
explicit operator bool() const {
return pImpl != nullptr;
}

Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/jit/passes/batch_mm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ struct TreeToken {
return token;
}

operator bool() {
explicit operator bool() {
return is_root;
}

Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/utils/object_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class THPPointer {
THPPointer& operator =(T *new_ptr) { free(); ptr = new_ptr; return *this; }
THPPointer& operator =(THPPointer &&p) { free(); ptr = p.ptr; p.ptr = nullptr; return *this; }
T * operator ->() { return ptr; }
operator bool() const { return ptr != nullptr; }
explicit operator bool() const { return ptr != nullptr; }

private:
void free();
Expand Down

0 comments on commit 18a9752

Please sign in to comment.