From 1adcf56bec7d2127d83aa423916639740fe8e586 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 13 Dec 2017 21:39:16 +0100 Subject: 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 --- apt-pkg/cdrom.cc | 2 +- apt-pkg/contrib/cdromutl.cc | 33 ++++++++++++++------------------- apt-pkg/indexcopy.cc | 15 +++++++-------- apt-private/private-source.cc | 7 +------ cmdline/apt-config.cc | 2 +- ftparchive/writer.cc | 5 +---- methods/basehttp.cc | 2 +- test/libapt/getlistoffilesindir_test.cc | 2 +- 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 &List,const char *Name) for (std::vector::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(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 &List, // Prepare the progress indicator off_t TotalSize = 0; std::vector const compressor = APT::Configuration::getCompressors(); - for (vector::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::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 &List, unsigned int WrongSize = 0; unsigned int Packages = 0; for (vector::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(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::const_iterator a = c->CompressArgs.begin(); a != c->CompressArgs.end(); ++a) _config->Set(comp + "CompressArg::", *a); for (std::vector::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 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; -- cgit v1.2.3