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 +++++++-------- 3 files changed, 22 insertions(+), 28 deletions(-) (limited to 'apt-pkg') 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(); -- cgit v1.2.3