Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A few clang-tidy findings addressed #2355

Merged
merged 2 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Checks: >
-readability-isolate-declaration,
-readability-magic-numbers,
-readability-make-member-function-const,
-readability-math-missing-parentheses,
-readability-named-parameter,
-readability-qualified-auto,
-readability-redundant-access-specifiers,
Expand Down
2 changes: 1 addition & 1 deletion verible/common/formatting/align.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ static int MaxOfPositives2D(const std::vector<std::vector<int>> &values) {
for (const auto &row : values) {
for (const int delta : row) {
// Only accumulate positive values.
if (delta > result) result = delta;
result = std::max(delta, result);
}
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion verible/common/formatting/layout-optimizer-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ class LayoutFunctionFactory {
//
// Iterator: iterator type that dereferences to LayoutFunction.
template <class Iterator>
LayoutFunction Choice(const Iterator begin, const Iterator end) const {
LayoutFunction Choice(const Iterator &begin, const Iterator &end) const {
static_assert(IsIteratorDereferencingTo<Iterator, LayoutFunction>,
"Iterator's value type must be LayoutFunction.");
const auto lfs = make_container_range(begin, end);
Expand Down
4 changes: 2 additions & 2 deletions verible/common/formatting/layout-optimizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ LayoutFunction LayoutFunctionFactory::Stack(
if ((segment_it + 1).IsEnd()) continue;
const int column = (segment_it + 1)->column;
CHECK_GE(column, current_column);
if (column < next_column) next_column = column;
next_column = std::min(column, next_column);
}
current_column = next_column;
}
Expand Down Expand Up @@ -434,7 +434,7 @@ LayoutFunction LayoutFunctionFactory::Choice(

const int column =
(segment_it + 1).IsEnd() ? kInfinity : segment_it[1].column;
if (column < next_knot) next_knot = column;
next_knot = std::min(column, next_knot);
}

do {
Expand Down
3 changes: 2 additions & 1 deletion verible/common/lsp/lsp-text-buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "verible/common/lsp/lsp-text-buffer.h"

#include <algorithm>
#include <numeric>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -86,7 +87,7 @@ bool EditTextBuffer::LineEdit(const TextDocumentContentChangeEvent &c,
if (!str->empty() && str->back() == '\n') --str_end;

if (c.range.start.character > str_end) return false;
if (end_char > str_end) end_char = str_end;
end_char = std::min(end_char, str_end);
if (end_char < c.range.start.character) return false;

document_length_ -= str->length();
Expand Down
14 changes: 7 additions & 7 deletions verible/common/lsp/message-stream-splitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ int MessageStreamSplitter::ParseHeaderGetBodyOffset(std::string_view data,
static constexpr std::string_view kEndHeaderMarker = "\r\n\r\n";
static constexpr std::string_view kContentLengthHeader = "Content-Length: ";

int header_marker_len = kEndHeaderMarker.length();
auto end_of_header = data.find(kEndHeaderMarker);
const size_t header_marker_len = kEndHeaderMarker.length();
const size_t end_of_header = data.find(kEndHeaderMarker);
if (end_of_header == std::string_view::npos) return kIncompleteHeader;

// Very dirty search for header - we don't check if starts with line.
const std::string_view header_content(data.data(), end_of_header);
const std::string_view header_content = data.substr(0, end_of_header);
auto found_ContentLength_header = header_content.find(kContentLengthHeader);
if (found_ContentLength_header == std::string_view::npos) {
return kGarbledHeader;
Expand All @@ -80,8 +80,8 @@ absl::Status MessageStreamSplitter::ProcessContainedMessages(
int body_size = 0;
const int body_offset = ParseHeaderGetBodyOffset(*data, &body_size);
if (body_offset == kGarbledHeader) {
std::string_view limited_view(
data->data(), std::min(data->size(), static_cast<size_t>(256)));
const std::string_view limited_view =
data->substr(0, std::min(data->size(), static_cast<size_t>(256)));
return absl::InvalidArgumentError(
absl::StrCat("No `Content-Length:` header. '",
absl::CEscape(limited_view), "...'"));
Expand All @@ -93,8 +93,8 @@ absl::Status MessageStreamSplitter::ProcessContainedMessages(
return absl::OkStatus(); // Only insufficient partial buffer available.
}

std::string_view header(data->data(), body_offset);
std::string_view body(data->data() + body_offset, body_size);
const std::string_view header = data->substr(0, body_offset);
const std::string_view body = data->substr(body_offset, body_size);
message_processor_(header, body);

stats_largest_body_ = std::max(stats_largest_body_, body.size());
Expand Down
2 changes: 1 addition & 1 deletion verible/common/text/macro-definition.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const TokenInfo &MacroDefinition::SubstituteText(
}
}
// Didn't match enum type nor find map entry, so don't substitute.
return token_info;
return token_info; // NOLINT(bugprone-return-const-ref-from-parameter)
}

} // namespace verible
6 changes: 2 additions & 4 deletions verible/common/text/tree-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ SyntaxTreeNode &SymbolCastToNode(Symbol &); // NOLINT
// The following no-op overloads allow SymbolCastToNode() to work with zero
// overhead when the argument type is statically known to be the same.
inline const SyntaxTreeNode &SymbolCastToNode(const SyntaxTreeNode &node) {
return node;
}
inline SyntaxTreeNode &SymbolCastToNode(SyntaxTreeNode &node) { // NOLINT
return node;
return node; // NOLINT(bugprone-return-const-ref-from-parameter)
}
inline SyntaxTreeNode &SymbolCastToNode(SyntaxTreeNode &node) { return node; }

// Returns a SyntaxTreeLeaf down_casted from a Symbol.
const SyntaxTreeLeaf &SymbolCastToLeaf(const Symbol &);
Expand Down
2 changes: 1 addition & 1 deletion verible/common/util/container-proxy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ struct TestTrace {
std::equal(values.begin(), values.end(), other.values.begin());
}

friend std::ostream &operator<<(std::ostream &s, TestTrace obj) {
friend std::ostream &operator<<(std::ostream &s, const TestTrace &obj) {
switch (obj.triggered_method) {
case ContainerProxyEvent::kUnknown:
s << "UNKNOWN";
Expand Down
3 changes: 2 additions & 1 deletion verible/common/util/container-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ struct ValueTypeFromKeyType<KeyType, std::pair<const KeyType, MappedType>> {
template <class KeyType>
struct ValueTypeFromKeyType<KeyType, KeyType> {
const KeyType &operator()(const KeyType &k) const {
return k; // just forward the key
// Just forward the key
return k; // NOLINT(bugprone-return-const-ref-from-parameter)
}
};
} // namespace internal
Expand Down
6 changes: 3 additions & 3 deletions verible/common/util/file-util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ absl::StatusOr<std::string> GetContentAsString(std::string_view filename) {
}
char buffer[4096];
int bytes_read;
do {
bytes_read = fread(buffer, 1, sizeof(buffer), stream);
while (!ferror(stream) && !feof(stream) &&
(bytes_read = fread(buffer, 1, sizeof(buffer), stream)) >= 0) {
content.append(buffer, bytes_read);
} while (bytes_read > 0);
}
fclose(stream);

return content;
Expand Down
4 changes: 3 additions & 1 deletion verible/common/util/forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ struct ForwardReferenceElseConstruct {
// Intentionally restricted to const reference to avoid the surprise
// of modifying a temporary reference.
// See also the two-parameter overload.
const T &operator()(const T &t) const { return t; }
const T &operator()(const T &t) const {
return t; // NOLINT(bugprone-return-const-ref-from-parameter)
}

// (overload) Constructs a temporary rvalue, when types are different.
// This works with explicit-only constructors and implicit constructors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const LintRuleDescriptor &ConstraintNameStyleRule::GetDescriptor() {
.desc =
"Check that constraint names follow the required name style "
"specified by a regular expression.",
.param = {{"pattern", kSuffix.data()}},
.param = {{"pattern", std::string(kSuffix)}},
};
return d;
}
Expand Down
3 changes: 2 additions & 1 deletion verible/verilog/analysis/verilog-linter-configuration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ void LinterConfiguration::GetRuleBundle(RuleBundle *rule_bundle) const {
template <typename T>
static absl::StatusOr<std::vector<std::unique_ptr<T>>> CreateRules(
const std::map<analysis::LintRuleId, RuleSetting> &config,
std::function<std::unique_ptr<T>(const analysis::LintRuleId &)> factory) {
const std::function<std::unique_ptr<T>(const analysis::LintRuleId &)>
&factory) {
std::vector<std::unique_ptr<T>> rule_instances;
for (const auto &rule_pair : config) {
const RuleSetting &setting = rule_pair.second;
Expand Down
4 changes: 1 addition & 3 deletions verible/verilog/formatting/token-annotator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,7 @@ static WithReason<int> SpacesRequiredBetween(
}

int spaces = right.OriginalLeadingSpaces().length();
if (spaces > 1) {
spaces = 1;
}
spaces = std::min(spaces, 1);
return {spaces, "Limit <= 1 space before binary operator inside []."};
}
if (left.format_token_enum == FormatTokenType::binary_operator &&
Expand Down
6 changes: 2 additions & 4 deletions verible/verilog/tools/ls/autoexpand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,8 @@ Dimension MaxDimension(const size_t first, const size_t second) {
template <>
Dimension MaxDimension(const DimensionRange first,
const DimensionRange second) {
int64_t max = std::max(std::max(first.msb, first.lsb),
std::max(second.msb, second.lsb));
int64_t min = std::min(std::min(first.msb, first.lsb),
std::min(second.msb, second.lsb));
int64_t max = std::max({first.msb, first.lsb, second.msb, second.lsb});
int64_t min = std::min({first.msb, first.lsb, second.msb, second.lsb});
if (first.msb >= first.lsb) {
return DimensionRange{.msb = max, .lsb = min};
}
Expand Down
Loading