Avoid unnecessary type names in return statements

This commit is contained in:
Tim Wojtulewicz 2023-07-06 14:07:43 -07:00
parent 4957dace64
commit de13bb6361
12 changed files with 35 additions and 36 deletions

View file

@ -267,28 +267,26 @@ size_t ODesc::StartsWithEscapeSequence(const char* start, const char* end)
std::pair<const char*, size_t> ODesc::FirstEscapeLoc(const char* bytes, size_t n) std::pair<const char*, size_t> ODesc::FirstEscapeLoc(const char* bytes, size_t n)
{ {
using escape_pos = std::pair<const char*, size_t>;
if ( IsBinary() ) if ( IsBinary() )
return escape_pos(0, 0); return {nullptr, 0};
for ( size_t i = 0; i < n; ++i ) for ( size_t i = 0; i < n; ++i )
{ {
auto printable = isprint(bytes[i]); auto printable = isprint(bytes[i]);
if ( ! printable && ! utf8 ) if ( ! printable && ! utf8 )
return escape_pos(bytes + i, 1); return {bytes + i, 1};
if ( bytes[i] == '\\' ) if ( bytes[i] == '\\' )
return escape_pos(bytes + i, 1); return {bytes + i, 1};
size_t len = StartsWithEscapeSequence(bytes + i, bytes + n); size_t len = StartsWithEscapeSequence(bytes + i, bytes + n);
if ( len ) if ( len )
return escape_pos(bytes + i, len); return {bytes + i, len};
} }
return escape_pos(nullptr, 0); return {nullptr, 0};
} }
void ODesc::AddBytes(const void* bytes, unsigned int n) void ODesc::AddBytes(const void* bytes, unsigned int n)
@ -429,7 +427,7 @@ std::string obj_desc(const Obj* o)
d.SP(); d.SP();
o->GetLocationInfo()->Describe(&d); o->GetLocationInfo()->Describe(&d);
return std::string(d.Description()); return d.Description();
} }
std::string obj_desc_short(const Obj* o) std::string obj_desc_short(const Obj* o)
@ -440,7 +438,7 @@ std::string obj_desc_short(const Obj* o)
d.Clear(); d.Clear();
o->Describe(&d); o->Describe(&d);
return std::string(d.Description()); return d.Description();
} }
} // namespace zeek } // namespace zeek

View file

@ -612,28 +612,29 @@ bool IPv6_Hdr_Chain::IsFragment() const
IPAddr IPv6_Hdr_Chain::SrcAddr() const IPAddr IPv6_Hdr_Chain::SrcAddr() const
{ {
if ( homeAddr ) if ( homeAddr )
return IPAddr(*homeAddr); return {*homeAddr};
if ( chain.empty() ) if ( chain.empty() )
{ {
reporter->InternalWarning("empty IPv6 header chain"); reporter->InternalWarning("empty IPv6 header chain");
return IPAddr(); return {};
} }
return IPAddr(((const struct ip6_hdr*)(chain[0]->Data()))->ip6_src); return IPAddr{((const struct ip6_hdr*)(chain[0]->Data()))->ip6_src};
} }
IPAddr IPv6_Hdr_Chain::DstAddr() const IPAddr IPv6_Hdr_Chain::DstAddr() const
{ {
if ( finalDst ) if ( finalDst )
return IPAddr(*finalDst); return {*finalDst};
if ( chain.empty() ) if ( chain.empty() )
{ {
reporter->InternalWarning("empty IPv6 header chain"); reporter->InternalWarning("empty IPv6 header chain");
return IPAddr(); return {};
} }
return IPAddr(((const struct ip6_hdr*)(chain[0]->Data()))->ip6_dst); return IPAddr{((const struct ip6_hdr*)(chain[0]->Data()))->ip6_dst};
} }
void IPv6_Hdr_Chain::ProcessRoutingHeader(const struct ip6_rthdr* r, uint16_t len) void IPv6_Hdr_Chain::ProcessRoutingHeader(const struct ip6_rthdr* r, uint16_t len)

View file

@ -203,7 +203,7 @@ std::string Specific_RE_Matcher::LookupDef(const std::string& def)
if ( iter != defs.end() ) if ( iter != defs.end() )
return iter->second; return iter->second;
return std::string(); return {};
} }
bool Specific_RE_Matcher::MatchAll(const char* s) bool Specific_RE_Matcher::MatchAll(const char* s)

View file

@ -956,13 +956,13 @@ const char* StringVal::CheckString() const
string StringVal::ToStdString() const string StringVal::ToStdString() const
{ {
auto* bs = AsString(); auto* bs = AsString();
return string((char*)bs->Bytes(), bs->Len()); return {(char*)bs->Bytes(), static_cast<size_t>(bs->Len())};
} }
string_view StringVal::ToStdStringView() const string_view StringVal::ToStdStringView() const
{ {
auto* bs = AsString(); auto* bs = AsString();
return string_view((char*)bs->Bytes(), bs->Len()); return {(char*)bs->Bytes(), static_cast<size_t>(bs->Len())};
} }
StringVal* StringVal::ToUpper() StringVal* StringVal::ToUpper()

View file

@ -24,7 +24,7 @@ type uint48 = record {
%code{ %code{
string orig_label(bool is_orig) string orig_label(bool is_orig)
{ {
return string(is_orig ? "originator" :"responder"); return is_orig ? "originator" :"responder";
} }
%} %}

View file

@ -26,13 +26,13 @@ enum AnalyzerState {
{ {
switch ( state_nr ) { switch ( state_nr ) {
case STATE_CLEAR: case STATE_CLEAR:
return string("CLEAR"); return "CLEAR";
case STATE_ENCRYPTED: case STATE_ENCRYPTED:
return string("ENCRYPTED"); return "ENCRYPTED";
default: default:
return string(zeek::util::fmt("UNKNOWN (%d)", state_nr)); return zeek::util::fmt("UNKNOWN (%d)", state_nr);
} }
} }
%} %}

View file

@ -44,7 +44,7 @@ FieldMapping::FieldMapping(const FieldMapping& arg)
FieldMapping FieldMapping::subType() FieldMapping FieldMapping::subType()
{ {
return FieldMapping(name, subtype, position); return {name, subtype, position};
} }
FieldMapping& FieldMapping::operator=(const FieldMapping& arg) FieldMapping& FieldMapping::operator=(const FieldMapping& arg)

View file

@ -39,7 +39,7 @@ string extract_module_name(const char* name)
string::size_type pos = module_name.rfind("::"); string::size_type pos = module_name.rfind("::");
if ( pos == string::npos ) if ( pos == string::npos )
return string(GLOBAL_MODULE_NAME); return GLOBAL_MODULE_NAME;
module_name.erase(pos); module_name.erase(pos);
@ -63,7 +63,7 @@ string extract_var_name(const char* name)
return var_name; return var_name;
if ( pos + 2 > var_name.size() ) if ( pos + 2 > var_name.size() )
return string(""); return "";
return var_name.substr(pos + 2); return var_name.substr(pos + 2);
} }
@ -81,7 +81,7 @@ string normalized_module_name(const char* module_name)
if ( (mod_len = strlen(module_name)) >= 2 && streq(module_name + mod_len - 2, "::") ) if ( (mod_len = strlen(module_name)) >= 2 && streq(module_name + mod_len - 2, "::") )
mod_len -= 2; mod_len -= 2;
return string(module_name, mod_len); return {module_name, static_cast<size_t>(mod_len)};
} }
TEST_CASE("module_util make_full_var_name") TEST_CASE("module_util make_full_var_name")
@ -103,7 +103,7 @@ string make_full_var_name(const char* module_name, const char* var_name)
if ( streq(GLOBAL_MODULE_NAME, extract_module_name(var_name).c_str()) ) if ( streq(GLOBAL_MODULE_NAME, extract_module_name(var_name).c_str()) )
return extract_var_name(var_name); return extract_var_name(var_name);
return string(var_name); return var_name;
} }
string full_name = normalized_module_name(module_name); string full_name = normalized_module_name(module_name);

View file

@ -104,7 +104,7 @@ char last_tok[128];
static std::string find_relative_file(const std::string& filename, const std::string& ext) static std::string find_relative_file(const std::string& filename, const std::string& ext)
{ {
if ( filename.empty() ) if ( filename.empty() )
return std::string(); return {};
if ( filename[0] == '.' ) if ( filename[0] == '.' )
return zeek::util::find_file(filename, zeek::util::SafeDirname(::filename).result, ext); return zeek::util::find_file(filename, zeek::util::SafeDirname(::filename).result, ext);
@ -115,7 +115,7 @@ static std::string find_relative_file(const std::string& filename, const std::st
static std::string find_relative_script_file(const std::string& filename) static std::string find_relative_script_file(const std::string& filename)
{ {
if ( filename.empty() ) if ( filename.empty() )
return std::string(); return {};
if ( filename[0] == '.' ) if ( filename[0] == '.' )
return zeek::util::find_script_file(filename, zeek::util::SafeDirname(::filename).result); return zeek::util::find_script_file(filename, zeek::util::SafeDirname(::filename).result);

View file

@ -352,7 +352,7 @@ std::string MsgThread::BuildMsgWithLocation(const char* msg)
} }
desc.Add(msg); desc.Add(msg);
return std::string(desc.Description()); return desc.Description();
} }
void MsgThread::Info(const char* msg) void MsgThread::Info(const char* msg)

View file

@ -2002,7 +2002,7 @@ static string find_file_in_path(const string& filename, const string& path,
const vector<string>& opt_ext) const vector<string>& opt_ext)
{ {
if ( filename.empty() ) if ( filename.empty() )
return string(); return {};
zeek::filesystem::path filepath(filename); zeek::filesystem::path filepath(filename);
@ -2012,7 +2012,7 @@ static string find_file_in_path(const string& filename, const string& path,
if ( can_read(filename) ) if ( can_read(filename) )
return filename; return filename;
else else
return string(); return {};
} }
auto abs_path = (zeek::filesystem::path(path) / filepath).string(); auto abs_path = (zeek::filesystem::path(path) / filepath).string();
@ -2031,7 +2031,7 @@ static string find_file_in_path(const string& filename, const string& path,
if ( can_read(abs_path) ) if ( can_read(abs_path) )
return abs_path; return abs_path;
return string(); return {};
} }
string find_file(const string& filename, const string& path_set, const string& opt_ext) string find_file(const string& filename, const string& path_set, const string& opt_ext)
@ -2051,7 +2051,7 @@ string find_file(const string& filename, const string& path_set, const string& o
return f; return f;
} }
return string(); return {};
} }
string find_script_file(const string& filename, const string& path_set) string find_script_file(const string& filename, const string& path_set)
@ -2069,7 +2069,7 @@ string find_script_file(const string& filename, const string& path_set)
return f; return f;
} }
return string(); return {};
} }
RETSIGTYPE sig_handler(int signo); RETSIGTYPE sig_handler(int signo);

View file

@ -67,7 +67,7 @@ vector<string> IdentifierInfo::GetFieldComments(const string& field) const
record_field_map::const_iterator it = fields.find(field); record_field_map::const_iterator it = fields.find(field);
if ( it == fields.end() ) if ( it == fields.end() )
return vector<string>(); return {};
return it->second->comments; return it->second->comments;
} }