diff options
author | David Kalnischkies <david@kalnischkies.de> | 2017-12-13 21:39:16 +0100 |
---|---|---|
committer | David Kalnischkies <david@kalnischkies.de> | 2017-12-13 23:53:41 +0100 |
commit | 1adcf56bec7d2127d83aa423916639740fe8e586 (patch) | |
tree | 39802989990796e903d8e670b07408380b117c21 | |
parent | 957381a0d26ec11a172ebfc64f892d1b31f0c193 (diff) |
avoid some useless casts reported by -Wuseless-cast
The casts are useless, but the reports show some where we can actually
improve the code by replacing them with better alternatives like
converting whatever int type into a string instead of casting to a
specific one which might in the future be too small.
Reported-By: gcc -Wuseless-cast
-rw-r--r-- | apt-pkg/cdrom.cc | 2 | ||||
-rw-r--r-- | apt-pkg/contrib/cdromutl.cc | 33 | ||||
-rw-r--r-- | apt-pkg/indexcopy.cc | 15 | ||||
-rw-r--r-- | apt-private/private-source.cc | 7 | ||||
-rw-r--r-- | cmdline/apt-config.cc | 2 | ||||
-rw-r--r-- | ftparchive/writer.cc | 5 | ||||
-rw-r--r-- | methods/basehttp.cc | 2 | ||||
-rw-r--r-- | test/libapt/getlistoffilesindir_test.cc | 2 | ||||
-rw-r--r-- | test/libapt/globalerror_test.cc | 4 |
9 files changed, 29 insertions, 43 deletions
diff --git a/apt-pkg/cdrom.cc b/apt-pkg/cdrom.cc index 4f57153f9..e4b0bef06 100644 --- a/apt-pkg/cdrom.cc +++ b/apt-pkg/cdrom.cc @@ -315,7 +315,7 @@ bool pkgCdrom::DropRepeats(vector<string> &List,const char *Name) for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin(); c != compressor.end(); ++c) { - std::string filename = std::string(List[I]).append(Name).append(c->Extension); + std::string const filename = List[I] + Name + c->Extension; if (stat(filename.c_str(), &Buf) != 0) continue; Inodes[I] = Buf.st_ino; diff --git a/apt-pkg/contrib/cdromutl.cc b/apt-pkg/contrib/cdromutl.cc index 93bfb9f42..7ae54060a 100644 --- a/apt-pkg/contrib/cdromutl.cc +++ b/apt-pkg/contrib/cdromutl.cc @@ -105,13 +105,14 @@ bool UnmountCdrom(string Path) } else { - const char *Args[10]; - Args[0] = "umount"; - Args[1] = Path.c_str(); - Args[2] = 0; - execvp(Args[0],(char **)Args); + const char * const Args[] = { + "umount", + Path.c_str(), + nullptr + }; + execvp(Args[0], const_cast<char **>(Args)); _exit(100); - } + } } // if it can not be umounted, give it a bit more time @@ -222,15 +223,13 @@ bool IdentCdrom(string CD,string &Res,unsigned int Version) std::string S; if (Version <= 1) - { - strprintf(S, "%lu", (unsigned long)Dir->d_ino); - } + S = std::to_string(Dir->d_ino); else { struct stat Buf; if (fstatat(dirfd, Dir->d_name, &Buf, 0) != 0) continue; - strprintf(S, "%lu", (unsigned long)Buf.st_mtime); + S = std::to_string(Buf.st_mtime); } Hash.Add(S.c_str()); @@ -246,21 +245,17 @@ bool IdentCdrom(string CD,string &Res,unsigned int Version) return _error->Errno("statfs",_("Failed to stat the cdrom")); // We use a kilobyte block size to avoid overflow - if (writable_media) - { - strprintf(S, "%lu", (unsigned long)(Buf.f_blocks*(Buf.f_bsize/1024))); - } else { - strprintf(S, "%lu %lu", (unsigned long)(Buf.f_blocks*(Buf.f_bsize/1024)), - (unsigned long)(Buf.f_bfree*(Buf.f_bsize/1024))); - } - Hash.Add(S.c_str()); + S = std::to_string(Buf.f_blocks * (Buf.f_bsize / 1024)); + if (writable_media == false) + S.append(" ").append(std::to_string(Buf.f_bfree * (Buf.f_bsize / 1024))); + Hash.Add(S.c_str(), S.length()); strprintf(S, "-%u", Version); } else strprintf(S, "-%u.debug", Version); closedir(D); - Res = Hash.Result().Value() + S; + Res = Hash.Result().Value().append(std::move(S)); return true; } /*}}}*/ diff --git a/apt-pkg/indexcopy.cc b/apt-pkg/indexcopy.cc index d4cab1a04..cb23a860f 100644 --- a/apt-pkg/indexcopy.cc +++ b/apt-pkg/indexcopy.cc @@ -58,15 +58,14 @@ bool IndexCopy::CopyPackages(string CDROM,string Name,vector<string> &List, // Prepare the progress indicator off_t TotalSize = 0; std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors(); - for (vector<string>::iterator I = List.begin(); I != List.end(); ++I) + for (auto const &F : List) { struct stat Buf; bool found = false; - std::string file = std::string(*I).append(GetFileName()); - for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin(); - c != compressor.end(); ++c) + auto const file = F + GetFileName(); + for (auto const &ext: APT::Configuration::getCompressorExtensions()) { - if (stat((file + c->Extension).c_str(), &Buf) != 0) + if (stat((file + ext).c_str(), &Buf) != 0) continue; found = true; break; @@ -82,9 +81,9 @@ bool IndexCopy::CopyPackages(string CDROM,string Name,vector<string> &List, unsigned int WrongSize = 0; unsigned int Packages = 0; for (vector<string>::iterator I = List.begin(); I != List.end(); ++I) - { - string OrigPath = string(*I,CDROM.length()); - + { + std::string OrigPath(*I,CDROM.length()); + // Open the package file FileFd Pkg(*I + GetFileName(), FileFd::ReadOnly, FileFd::Auto); off_t const FileSize = Pkg.Size(); diff --git a/apt-private/private-source.cc b/apt-private/private-source.cc index 47610cd80..32651cfdb 100644 --- a/apt-private/private-source.cc +++ b/apt-private/private-source.cc @@ -208,12 +208,7 @@ static pkgSrcRecords::Parser *FindSrc(const char *Name, // or RelTag if (Cache.BuildPolicy() == false) return nullptr; - pkgPolicy * Policy = dynamic_cast<pkgPolicy*>(Cache.GetPolicy()); - if (Policy == nullptr) - { - _error->Fatal("Implementation error: dynamic up-casting policy engine failed in FindSrc!"); - return nullptr; - } + pkgPolicy * const Policy = Cache.GetPolicy(); pkgCache::VerIterator const Ver = Policy->GetCandidateVer(Pkg); if (Ver.end() == false) { diff --git a/cmdline/apt-config.cc b/cmdline/apt-config.cc index ef1e95de1..4e546b098 100644 --- a/cmdline/apt-config.cc +++ b/cmdline/apt-config.cc @@ -128,7 +128,7 @@ int main(int argc,const char *argv[]) /*{{{*/ _config->Set(comp + "Name", c->Name); _config->Set(comp + "Extension", c->Extension); _config->Set(comp + "Binary", c->Binary); - _config->Set(std::string(comp + "Cost").c_str(), c->Cost); + _config->Set((comp + "Cost").c_str(), c->Cost); for (std::vector<std::string>::const_iterator a = c->CompressArgs.begin(); a != c->CompressArgs.end(); ++a) _config->Set(comp + "CompressArg::", *a); for (std::vector<std::string>::const_iterator a = c->UncompressArgs.begin(); a != c->UncompressArgs.end(); ++a) diff --git a/ftparchive/writer.cc b/ftparchive/writer.cc index bb855d532..91cb72d01 100644 --- a/ftparchive/writer.cc +++ b/ftparchive/writer.cc @@ -477,10 +477,7 @@ bool PackagesWriter::DoPackage(string FileName) // This lists all the changes to the fields we are going to make. std::vector<pkgTagSection::Tag> Changes; - - std::string Size; - strprintf(Size, "%llu", (unsigned long long) FileSize); - Changes.push_back(pkgTagSection::Tag::Rewrite("Size", Size)); + Changes.push_back(pkgTagSection::Tag::Rewrite("Size", std::to_string(FileSize))); for (HashStringList::const_iterator hs = Db.HashesList.begin(); hs != Db.HashesList.end(); ++hs) { diff --git a/methods/basehttp.cc b/methods/basehttp.cc index b4c9cc5ed..c77ab28c6 100644 --- a/methods/basehttp.cc +++ b/methods/basehttp.cc @@ -191,7 +191,7 @@ bool RequestState::HeaderLine(string const &Line) /*{{{*/ ; // we got the expected filesize which is all we wanted else if (sscanf(Val.c_str(),"bytes %llu-%*u/%llu",&StartPos,&TotalFileSize) != 2) return _error->Error(_("The HTTP server sent an invalid Content-Range header")); - if ((unsigned long long)StartPos > TotalFileSize) + if (StartPos > TotalFileSize) return _error->Error(_("This HTTP server has broken range support")); // figure out what we will download diff --git a/test/libapt/getlistoffilesindir_test.cc b/test/libapt/getlistoffilesindir_test.cc index 606e2733e..ba4f9f6ef 100644 --- a/test/libapt/getlistoffilesindir_test.cc +++ b/test/libapt/getlistoffilesindir_test.cc @@ -11,7 +11,7 @@ #include "file-helpers.h" -#define P(x) std::string(tempdir).append("/").append(x) +#define P(x) tempdir + "/" + x TEST(FileUtlTest,GetListOfFilesInDir) { diff --git a/test/libapt/globalerror_test.cc b/test/libapt/globalerror_test.cc index 67682c275..ff14d4618 100644 --- a/test/libapt/globalerror_test.cc +++ b/test/libapt/globalerror_test.cc @@ -111,11 +111,11 @@ TEST(GlobalErrorTest,LongMessage) longText.append("a"); EXPECT_FALSE(e.Error("%s horrible %s %d times", longText.c_str(), "happened", 2)); EXPECT_TRUE(e.PopMessage(text)); - EXPECT_EQ(std::string(longText).append(" horrible happened 2 times"), text); + EXPECT_EQ(longText + " horrible happened 2 times", text); EXPECT_FALSE(e.Errno("errno", "%s horrible %s %d times", longText.c_str(), "happened", 2)); EXPECT_TRUE(e.PopMessage(text)); - EXPECT_EQ(std::string(longText).append(" horrible happened 2 times - errno (0: ").append(textOfErrnoZero).append(")"), text); + EXPECT_EQ(longText + " horrible happened 2 times - errno (0: " + textOfErrnoZero + ")", text); EXPECT_FALSE(e.Error("%s horrible %s %d times", longText.c_str(), "happened", 2)); std::ostringstream out; |