Skip to content

Commit d6d1658

Browse files
authored
Address 'modernize-use-nodiscard' clang-tidy remarks (#251)
1 parent c54ed5b commit d6d1658

16 files changed

Lines changed: 76 additions & 65 deletions

File tree

.clang-tidy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Checks: >
1515
-modernize-return-braced-init-list,
1616
-modernize-use-trailing-return-type,
1717
-modernize-concat-nested-namespaces,
18-
-modernize-use-nodiscard,
1918
-modernize-avoid-c-arrays,
2019
-modernize-pass-by-value,
2120
-performance-move-const-arg,

include/graph/graph.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,30 +96,30 @@ class Graph {
9696
return raw_ptr;
9797
}
9898

99-
int getVertexValue(size_t layerID) const {
99+
[[nodiscard]] int getVertexValue(size_t layerID) const {
100100
if (layerID >= arrayV_.size()) {
101101
throw std::invalid_argument("ArrayV does not contain this ID.");
102102
}
103103
return arrayV_[layerID];
104104
}
105105

106-
int getEdgeValue(size_t pos) const {
106+
[[nodiscard]] int getEdgeValue(size_t pos) const {
107107
if (pos >= arrayE_.size()) {
108108
throw std::invalid_argument("ArrayE does not contain this.");
109109
}
110110
return arrayE_[pos];
111111
}
112112

113-
size_t getInputsSize(size_t layerID) const {
113+
[[nodiscard]] size_t getInputsSize(size_t layerID) const {
114114
if (layerID >= in_edges_.size()) {
115115
throw std::invalid_argument("Input edges array do not contain this ID.");
116116
}
117117
return in_edges_[layerID].size();
118118
}
119119

120-
int getLayersCount() const { return V_; }
120+
[[nodiscard]] int getLayersCount() const { return V_; }
121121

122-
const Layer& getLayerFromID(size_t layerID) const {
122+
[[nodiscard]] const Layer& getLayerFromID(size_t layerID) const {
123123
if (layerID >= layers_.size()) {
124124
throw std::invalid_argument("Layers do not contain this ID.");
125125
}
@@ -376,7 +376,7 @@ class Graph {
376376
std::vector<Tensor> getWEIGHTS() { return weights_; }
377377
#endif
378378

379-
std::vector<std::pair<int, int>> getInOutDegrees() const {
379+
[[nodiscard]] std::vector<std::pair<int, int>> getInOutDegrees() const {
380380
std::vector<int> in_degree(V_, 0);
381381

382382
for (int i = 0; i < V_; ++i) {
@@ -397,7 +397,7 @@ class Graph {
397397
return result;
398398
}
399399

400-
std::vector<int> getTraversalOrder() const {
400+
[[nodiscard]] std::vector<int> getTraversalOrder() const {
401401
auto in_out_degrees = getInOutDegrees();
402402
std::vector<int> in_degree(V_);
403403
for (int i = 0; i < V_; ++i) {

include/layers/ConcatLayer.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ class ConcatLayer : public Layer {
2525
int64_t axis_;
2626
std::vector<int> input_order_;
2727
void validate_inputs(const std::vector<Tensor>& inputs) const;
28-
int64_t normalize_axis(size_t rank) const;
29-
Shape calculate_output_shape(const std::vector<Tensor>& inputs) const;
30-
std::vector<Tensor> reorderInputs(const std::vector<Tensor>& inputs) const;
28+
[[nodiscard]] int64_t normalize_axis(size_t rank) const;
29+
[[nodiscard]] Shape calculate_output_shape(
30+
const std::vector<Tensor>& inputs) const;
31+
[[nodiscard]] std::vector<Tensor> reorderInputs(
32+
const std::vector<Tensor>& inputs) const;
3133
template <typename T>
3234
void concatenate(const std::vector<Tensor>& inputs, Tensor& output) const {
3335
std::vector<Tensor> ordered_inputs = reorderInputs(inputs);

include/layers/ConvLayer.hpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,17 @@ class ConvImpl : public LayerImpl<ValueType> {
8686

8787
ConvImpl(const ConvImpl& c) = default;
8888

89-
std::vector<ValueType> run(
89+
[[nodiscard]] std::vector<ValueType> run(
9090
const std::vector<ValueType>& input) const override {
9191
return input;
9292
}
9393

94-
std::vector<ValueType> run(std::vector<ValueType> startmatrix, int new_rows,
95-
int new_cols, std::vector<ValueType> startkernel,
96-
size_t start_kernel_size, size_t kernel_size,
97-
int center_distance) const {
94+
[[nodiscard]] std::vector<ValueType> run(std::vector<ValueType> startmatrix,
95+
int new_rows, int new_cols,
96+
std::vector<ValueType> startkernel,
97+
size_t start_kernel_size,
98+
size_t kernel_size,
99+
int center_distance) const {
98100
std::vector<ValueType> matrix(new_rows * new_cols * input_flow_, 0);
99101
for (int i = 0; i < input_height_; ++i) {
100102
for (int j = 0; j < input_width_; ++j) {
@@ -125,14 +127,14 @@ class ConvImpl : public LayerImpl<ValueType> {
125127
if (input_width_ == 0) {
126128
throw std::out_of_range("Input = 0");
127129
}
128-
int kercol_index = coloms / input_width_ + 1;
129-
if (kercol_index < 0) {
130-
throw std::out_of_range("Kernel column index is negative");
131-
}
132-
auto kercol = static_cast<size_t>(kercol_index);
133-
color +=
134-
matrix[(i + coloms + str) * input_flow_ + x] *
135-
kernel[kercol * kernel_size + static_cast<size_t>(str + 1)];
130+
int kercol_index = coloms / input_width_ + 1;
131+
if (kercol_index < 0) {
132+
throw std::out_of_range("Kernel column index is negative");
133+
}
134+
auto kercol = static_cast<size_t>(kercol_index);
135+
color +=
136+
matrix[(i + coloms + str) * input_flow_ + x] *
137+
kernel[kercol * kernel_size + static_cast<size_t>(str + 1)];
136138
}
137139
}
138140
if (!bias_.empty() && static_cast<size_t>(x) < bias_.size()) {

include/layers/FCLayer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class FCLayerImpl : public LayerImpl<ValueType> {
8484
}
8585
weights_[i * this->inputShape_[0] + j] = value;
8686
}
87-
ValueType get_weight(size_t i, size_t j) const {
87+
[[nodiscard]] ValueType get_weight(size_t i, size_t j) const {
8888
if (i >= this->outputShape_[0] || j >= this->inputShape_[0]) {
8989
throw std::out_of_range("Invalid weight index");
9090
}
@@ -96,7 +96,7 @@ class FCLayerImpl : public LayerImpl<ValueType> {
9696
}
9797
bias_[i] = value;
9898
}
99-
ValueType get_bias(size_t i) const {
99+
[[nodiscard]] ValueType get_bias(size_t i) const {
100100
if (i >= this->outputShape_[0]) {
101101
throw std::out_of_range("Invalid bias index");
102102
}

include/layers/Layer.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@ class Layer {
5252
explicit Layer(LayerType type) : type_(type) {}
5353
virtual ~Layer() = default;
5454
PostOperations postops;
55-
int getID() const { return id_; }
55+
[[nodiscard]] int getID() const { return id_; }
5656
void setID(int id) { id_ = id; }
5757
void setParallelBackend(ParBackend backend) { parallel_backend_ = backend; }
58-
ParBackend getParallelBackend() const { return parallel_backend_; }
59-
LayerType getName() const { return type_; }
58+
[[nodiscard]] ParBackend getParallelBackend() const {
59+
return parallel_backend_;
60+
}
61+
[[nodiscard]] LayerType getName() const { return type_; }
6062
virtual void run(const std::vector<Tensor>& input,
6163
std::vector<Tensor>& output) = 0;
6264
#ifdef ENABLE_STATISTIC_WEIGHTS
@@ -77,12 +79,12 @@ class LayerImpl {
7779
: inputShape_(inputShape), outputShape_(outputShape) {}
7880
LayerImpl(const LayerImpl& c) = default;
7981
LayerImpl& operator=(const LayerImpl& c) = default;
80-
virtual std::vector<ValueType> run(
82+
[[nodiscard]] virtual std::vector<ValueType> run(
8183
const std::vector<ValueType>& input) const = 0;
82-
Shape get_input_shape() const { return inputShape_; }
83-
Shape get_output_shape() const { return outputShape_; }
84+
[[nodiscard]] Shape get_input_shape() const { return inputShape_; }
85+
[[nodiscard]] Shape get_output_shape() const { return outputShape_; }
8486
// weights width x height
85-
std::pair<Shape, Shape> get_dims() const {
87+
[[nodiscard]] std::pair<Shape, Shape> get_dims() const {
8688
return std::pair<Shape, Shape>(outputShape_, inputShape_);
8789
}
8890

include/layers/OutputLayer.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class OutputLayer : public Layer {
1818
std::vector<Tensor>& output) override {
1919
output = input;
2020
}
21-
std::vector<std::string> get_labels() const { return labels_; }
22-
std::pair<std::vector<std::string>, Tensor> top_k(const Tensor& input,
23-
size_t k) const;
21+
[[nodiscard]] std::vector<std::string> get_labels() const { return labels_; }
22+
[[nodiscard]] std::pair<std::vector<std::string>, Tensor> top_k(
23+
const Tensor& input, size_t k) const;
2424
#ifdef ENABLE_STATISTIC_WEIGHTS
2525
Tensor get_weights() override {
2626
std::vector<int> v = {0};

include/layers/ReduceLayer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class ReduceLayer : public Layer {
3232

3333
static void normalize_axes(const Shape& input_shape,
3434
std::vector<int64_t>& axes);
35-
Shape calculate_output_shape(const Shape& input_shape,
36-
const std::vector<int64_t>& axes) const;
35+
[[nodiscard]] Shape calculate_output_shape(
36+
const Shape& input_shape, const std::vector<int64_t>& axes) const;
3737

3838
template <typename T>
3939
void compute(const Tensor& input, const Shape& output_shape,

include/layers/Shape.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Shape {
2020
Shape& operator=(const Shape& c) = default;
2121
size_t operator[](size_t i) const noexcept { return dims_[i]; }
2222
size_t& operator[](size_t i) noexcept { return dims_[i]; }
23-
size_t at(size_t i) const {
23+
[[nodiscard]] size_t at(size_t i) const {
2424
if (i >= dims_.size()) {
2525
throw std::out_of_range("Invalid shape index");
2626
}
@@ -33,12 +33,12 @@ class Shape {
3333
return dims_[i];
3434
}
3535
void resize(const std::vector<size_t>& new_size) { dims_ = new_size; }
36-
size_t count() const {
36+
[[nodiscard]] size_t count() const {
3737
return std::accumulate(dims_.begin(), dims_.end(), static_cast<size_t>(1),
3838
std::multiplies<>());
3939
}
40-
size_t dims() const noexcept { return dims_.size(); }
41-
size_t get_index(const std::vector<size_t>& coords) const;
40+
[[nodiscard]] size_t dims() const noexcept { return dims_.size(); }
41+
[[nodiscard]] size_t get_index(const std::vector<size_t>& coords) const;
4242
bool operator==(const Shape& other) const {
4343
if (dims_.size() != other.dims_.size()) return false;
4444
for (size_t i = 0; i < dims_.size(); ++i) {

include/layers/SoftmaxLayer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class SoftmaxLayer : public Layer {
2121
#endif
2222

2323
void set_axis(int axis) { axis_ = axis; }
24-
int get_axis() const { return axis_; }
24+
[[nodiscard]] int get_axis() const { return axis_; }
2525

2626
private:
2727
int axis_;

0 commit comments

Comments
 (0)