lib: Drop intermediary return variables

In a few places we declare a variable (which is optionally
followed by a code not touching it) then set the variable to a
value and return the variable immediately. It's obvious that the
variable is needless and the value can be returned directly
instead.

This patch was generated using this semantic patch:

  @@
  type T;
  identifier ret;
  expression E;
  @@
  - T ret;
  ... when != ret
      when strict
  - ret = E;
  - return ret;
  + return E;

After that I fixed couple of formatting issues because coccinelle
formatted some lines differently than our coding style.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Michal Privoznik 2021-10-22 10:56:01 +02:00
parent 945942ebba
commit 857df2fe50
34 changed files with 54 additions and 190 deletions

View File

@ -36,11 +36,7 @@ VIR_LOG_INIT("bhyve.bhyve_domain");
static void * static void *
bhyveDomainObjPrivateAlloc(void *opaque G_GNUC_UNUSED) bhyveDomainObjPrivateAlloc(void *opaque G_GNUC_UNUSED)
{ {
bhyveDomainObjPrivate *priv; return g_new0(bhyveDomainObjPrivate, 1);
priv = g_new0(bhyveDomainObjPrivate, 1);
return priv;
} }
static void static void

View File

@ -1862,11 +1862,7 @@ virDomainUSBAddressPortFormat(unsigned int *port)
virDomainUSBAddressSet * virDomainUSBAddressSet *
virDomainUSBAddressSetCreate(void) virDomainUSBAddressSetCreate(void)
{ {
virDomainUSBAddressSet *addrs; return g_new0(virDomainUSBAddressSet, 1);
addrs = g_new0(virDomainUSBAddressSet, 1);
return addrs;
} }

View File

@ -28879,10 +28879,7 @@ char
*virDomainConfigFile(const char *dir, *virDomainConfigFile(const char *dir,
const char *name) const char *name)
{ {
char *ret; return g_strdup_printf("%s/%s.xml", dir, name);
ret = g_strdup_printf("%s/%s.xml", dir, name);
return ret;
} }
/* Translates a device name of the form (regex) "[fhv]d[a-z]+" into /* Translates a device name of the form (regex) "[fhv]d[a-z]+" into

View File

@ -1601,9 +1601,7 @@ char *
virNetworkObjGetPortStatusDir(virNetworkObj *net, virNetworkObjGetPortStatusDir(virNetworkObj *net,
const char *stateDir) const char *stateDir)
{ {
char *ret; return g_strdup_printf("%s/%s/ports", stateDir, net->def->name);
ret = g_strdup_printf("%s/%s/ports", stateDir, net->def->name);
return ret;
} }
int int

View File

@ -138,10 +138,7 @@ char *
virNWFilterBindingObjConfigFile(const char *dir, virNWFilterBindingObjConfigFile(const char *dir,
const char *name) const char *name)
{ {
char *ret; return g_strdup_printf("%s/%s.xml", dir, name);
ret = g_strdup_printf("%s/%s.xml", dir, name);
return ret;
} }

View File

@ -117,10 +117,7 @@ virNWFilterObjListFree(virNWFilterObjList *nwfilters)
virNWFilterObjList * virNWFilterObjList *
virNWFilterObjListNew(void) virNWFilterObjListNew(void)
{ {
virNWFilterObjList *nwfilters; return g_new0(virNWFilterObjList, 1);
nwfilters = g_new0(virNWFilterObjList, 1);
return nwfilters;
} }

View File

@ -1800,10 +1800,7 @@ esxDomainDestroy(virDomainPtr dom)
static char * static char *
esxDomainGetOSType(virDomainPtr domain G_GNUC_UNUSED) esxDomainGetOSType(virDomainPtr domain G_GNUC_UNUSED)
{ {
char *osType; return g_strdup("hvm");
osType = g_strdup("hvm");
return osType;
} }

View File

@ -766,10 +766,7 @@ esxStorageVolWipe(virStorageVolPtr volume G_GNUC_UNUSED,
static char * static char *
esxStorageVolGetPath(virStorageVolPtr volume) esxStorageVolGetPath(virStorageVolPtr volume)
{ {
char *path; return g_strdup(volume->name);
path = g_strdup(volume->name);
return path;
} }

View File

@ -1392,10 +1392,7 @@ esxStorageVolGetXMLDesc(virStorageVolPtr volume,
static char * static char *
esxStorageVolGetPath(virStorageVolPtr volume) esxStorageVolGetPath(virStorageVolPtr volume)
{ {
char *path; return g_strdup_printf("[%s] %s", volume->pool, volume->name);
path = g_strdup_printf("[%s] %s", volume->pool, volume->name);
return path;
} }

View File

@ -2234,10 +2234,7 @@ hypervDomainDestroy(virDomainPtr domain)
static char * static char *
hypervDomainGetOSType(virDomainPtr domain G_GNUC_UNUSED) hypervDomainGetOSType(virDomainPtr domain G_GNUC_UNUSED)
{ {
char *osType; return g_strdup("hvm");
osType = g_strdup("hvm");
return osType;
} }

View File

@ -616,10 +616,7 @@ virLockDaemonExecRestartStatePath(bool privileged,
static char * static char *
virLockDaemonGetExecRestartMagic(void) virLockDaemonGetExecRestartMagic(void)
{ {
char *ret; return g_strdup_printf("%lld", (long long int)getpid());
ret = g_strdup_printf("%lld", (long long int)getpid());
return ret;
} }

View File

@ -174,13 +174,11 @@ static int
virLockManagerSanlockInitLockspace(virLockManagerSanlockDriver *driver, virLockManagerSanlockInitLockspace(virLockManagerSanlockDriver *driver,
struct sanlk_lockspace *ls) struct sanlk_lockspace *ls)
{ {
int ret;
const int max_hosts = 0; /* defaults used in sanlock_init() implementation */ const int max_hosts = 0; /* defaults used in sanlock_init() implementation */
const unsigned int lockspaceFlags = 0; const unsigned int lockspaceFlags = 0;
ret = sanlock_write_lockspace(ls, max_hosts, lockspaceFlags, driver->io_timeout); return sanlock_write_lockspace(ls, max_hosts, lockspaceFlags,
return ret; driver->io_timeout);
} }
/* How many times try adding a lockspace? */ /* How many times try adding a lockspace? */

View File

@ -423,10 +423,7 @@ virLogDaemonExecRestartStatePath(bool privileged,
static char * static char *
virLogDaemonGetExecRestartMagic(void) virLogDaemonGetExecRestartMagic(void)
{ {
char *ret; return g_strdup_printf("%lld", (long long int)getpid());
ret = g_strdup_printf("%lld", (long long int)getpid());
return ret;
} }

View File

@ -365,10 +365,7 @@ static char *
networkDnsmasqLeaseFileNameDefault(virNetworkDriverState *driver, networkDnsmasqLeaseFileNameDefault(virNetworkDriverState *driver,
const char *netname) const char *netname)
{ {
char *leasefile; return g_strdup_printf("%s/%s.leases", driver->dnsmasqStateDir, netname);
leasefile = g_strdup_printf("%s/%s.leases", driver->dnsmasqStateDir, netname);
return leasefile;
} }
@ -376,10 +373,7 @@ static char *
networkDnsmasqLeaseFileNameCustom(virNetworkDriverState *driver, networkDnsmasqLeaseFileNameCustom(virNetworkDriverState *driver,
const char *bridge) const char *bridge)
{ {
char *leasefile; return g_strdup_printf("%s/%s.status", driver->dnsmasqStateDir, bridge);
leasefile = g_strdup_printf("%s/%s.status", driver->dnsmasqStateDir, bridge);
return leasefile;
} }
@ -387,21 +381,13 @@ static char *
networkDnsmasqConfigFileName(virNetworkDriverState *driver, networkDnsmasqConfigFileName(virNetworkDriverState *driver,
const char *netname) const char *netname)
{ {
char *conffile; return g_strdup_printf("%s/%s.conf", driver->dnsmasqStateDir, netname);
conffile = g_strdup_printf("%s/%s.conf", driver->dnsmasqStateDir, netname);
return conffile;
} }
static char * static char *
networkRadvdPidfileBasename(const char *netname) networkRadvdPidfileBasename(const char *netname)
{ {return g_strdup_printf("%s-radvd", netname);
/* this is simple but we want to be sure it's consistently done */
char *pidfilebase;
pidfilebase = g_strdup_printf("%s-radvd", netname);
return pidfilebase;
} }
@ -409,10 +395,7 @@ static char *
networkRadvdConfigFileName(virNetworkDriverState *driver, networkRadvdConfigFileName(virNetworkDriverState *driver,
const char *netname) const char *netname)
{ {
char *configfile; return g_strdup_printf("%s/%s-radvd.conf", driver->radvdStateDir, netname);
configfile = g_strdup_printf("%s/%s-radvd.conf", driver->radvdStateDir, netname);
return configfile;
} }

View File

@ -804,11 +804,7 @@ qemuAliasFromHostdev(const virDomainHostdevDef *hostdev)
char * char *
qemuDomainGetMasterKeyAlias(void) qemuDomainGetMasterKeyAlias(void)
{ {
char *alias; return g_strdup("masterKey0");
alias = g_strdup("masterKey0");
return alias;
} }
@ -837,11 +833,7 @@ qemuAliasForSecret(const char *parentalias,
char * char *
qemuAliasTLSObjFromSrcAlias(const char *srcAlias) qemuAliasTLSObjFromSrcAlias(const char *srcAlias)
{ {
char *ret; return g_strdup_printf("obj%s_tls0", srcAlias);
ret = g_strdup_printf("obj%s_tls0", srcAlias);
return ret;
} }
@ -853,11 +845,7 @@ qemuAliasTLSObjFromSrcAlias(const char *srcAlias)
char * char *
qemuAliasChardevFromDevAlias(const char *devAlias) qemuAliasChardevFromDevAlias(const char *devAlias)
{ {
char *ret; return g_strdup_printf("char%s", devAlias);
ret = g_strdup_printf("char%s", devAlias);
return ret;
} }
@ -871,11 +859,7 @@ qemuDomainGetManagedPRAlias(void)
char * char *
qemuDomainGetUnmanagedPRAlias(const char *parentalias) qemuDomainGetUnmanagedPRAlias(const char *parentalias)
{ {
char *ret; return g_strdup_printf("pr-helper-%s", parentalias);
ret = g_strdup_printf("pr-helper-%s", parentalias);
return ret;
} }

View File

@ -3811,18 +3811,15 @@ qemuBuildMemoryDeviceProps(const virDomainDef *def,
static char * static char *
qemuBuildLegacyNicStr(virDomainNetDef *net) qemuBuildLegacyNicStr(virDomainNetDef *net)
{ {
char *str;
char macaddr[VIR_MAC_STRING_BUFLEN]; char macaddr[VIR_MAC_STRING_BUFLEN];
const char *netmodel = virDomainNetGetModelString(net); const char *netmodel = virDomainNetGetModelString(net);
str = g_strdup_printf("nic,macaddr=%s,netdev=host%s%s%s%s%s", return g_strdup_printf("nic,macaddr=%s,netdev=host%s%s%s%s%s",
virMacAddrFormat(&net->mac, macaddr), virMacAddrFormat(&net->mac, macaddr),
net->info.alias, net->info.alias, netmodel ? ",model=" : "",
netmodel ? ",model=" : "", NULLSTR_EMPTY(netmodel),
NULLSTR_EMPTY(netmodel), (net->info.alias ? ",id=" : ""),
(net->info.alias ? ",id=" : ""), NULLSTR_EMPTY(net->info.alias));
NULLSTR_EMPTY(net->info.alias));
return str;
} }

View File

@ -5944,13 +5944,9 @@ qemuDomainDefFromXML(virQEMUDriver *driver,
virQEMUCaps *qemuCaps, virQEMUCaps *qemuCaps,
const char *xml) const char *xml)
{ {
virDomainDef *def; return virDomainDefParseString(xml, driver->xmlopt, qemuCaps,
VIR_DOMAIN_DEF_PARSE_INACTIVE |
def = virDomainDefParseString(xml, driver->xmlopt, qemuCaps, VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE);
VIR_DOMAIN_DEF_PARSE_INACTIVE |
VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE);
return def;
} }

View File

@ -920,10 +920,7 @@ qemuMonitorClose(qemuMonitor *mon)
char * char *
qemuMonitorNextCommandID(qemuMonitor *mon) qemuMonitorNextCommandID(qemuMonitor *mon)
{ {
char *id; return g_strdup_printf("libvirt-%d", ++mon->nextSerial);
id = g_strdup_printf("libvirt-%d", ++mon->nextSerial);
return id;
} }

View File

@ -1169,10 +1169,7 @@ static char *
AppArmorGetMountOptions(virSecurityManager *mgr G_GNUC_UNUSED, AppArmorGetMountOptions(virSecurityManager *mgr G_GNUC_UNUSED,
virDomainDef *vm G_GNUC_UNUSED) virDomainDef *vm G_GNUC_UNUSED)
{ {
char *opts; return g_strdup("");
opts = g_strdup("");
return opts;
} }
static const char * static const char *

View File

@ -178,10 +178,7 @@ static char *
virSecurityDomainGetMountOptionsNop(virSecurityManager *mgr G_GNUC_UNUSED, virSecurityDomainGetMountOptionsNop(virSecurityManager *mgr G_GNUC_UNUSED,
virDomainDef *vm G_GNUC_UNUSED) virDomainDef *vm G_GNUC_UNUSED)
{ {
char *opts; return g_strdup("");
opts = g_strdup("");
return opts;
} }
static const char * static const char *

View File

@ -1666,7 +1666,6 @@ static char *
testConnectGetSysinfo(virConnectPtr conn G_GNUC_UNUSED, testConnectGetSysinfo(virConnectPtr conn G_GNUC_UNUSED,
unsigned int flags) unsigned int flags)
{ {
char *ret;
const char *sysinfo = "<sysinfo type='smbios'>\n" const char *sysinfo = "<sysinfo type='smbios'>\n"
" <bios>\n" " <bios>\n"
" <entry name='vendor'>LENOVO</entry>\n" " <entry name='vendor'>LENOVO</entry>\n"
@ -1678,8 +1677,7 @@ testConnectGetSysinfo(virConnectPtr conn G_GNUC_UNUSED,
virCheckFlags(0, NULL); virCheckFlags(0, NULL);
ret = g_strdup(sysinfo); return g_strdup(sysinfo);
return ret;
} }
static const char * static const char *
@ -2706,10 +2704,7 @@ testDomainCoreDump(virDomainPtr domain,
static char * static char *
testDomainGetOSType(virDomainPtr dom G_GNUC_UNUSED) testDomainGetOSType(virDomainPtr dom G_GNUC_UNUSED)
{ {
char *ret; return g_strdup("linux");
ret = g_strdup("linux");
return ret;
} }

View File

@ -140,8 +140,6 @@ char *virAuditEncode(const char *key, const char *value)
#if WITH_AUDIT #if WITH_AUDIT
return audit_encode_nv_string(key, value, 0); return audit_encode_nv_string(key, value, 0);
#else #else
char *str; return g_strdup_printf("%s=%s", key, value);
str = g_strdup_printf("%s=%s", key, value);
return str;
#endif #endif
} }

View File

@ -157,11 +157,7 @@ virFirewallSetBackend(virFirewallBackend backend)
static virFirewallGroup * static virFirewallGroup *
virFirewallGroupNew(void) virFirewallGroupNew(void)
{ {
virFirewallGroup *group; return g_new0(virFirewallGroup, 1);
group = g_new0(virFirewallGroup, 1);
return group;
} }

View File

@ -267,11 +267,7 @@ char *
virMacMapFileName(const char *dnsmasqStateDir, virMacMapFileName(const char *dnsmasqStateDir,
const char *bridge) const char *bridge)
{ {
char *filename; return g_strdup_printf("%s/%s.macs", dnsmasqStateDir, bridge);
filename = g_strdup_printf("%s/%s.macs", dnsmasqStateDir, bridge);
return filename;
} }

View File

@ -2803,10 +2803,7 @@ static int virNetDevGetMulticastTable(const char *ifname,
virNetDevRxFilter * virNetDevRxFilter *
virNetDevRxFilterNew(void) virNetDevRxFilterNew(void)
{ {
virNetDevRxFilter *filter; return g_new0(virNetDevRxFilter, 1);
filter = g_new0(virNetDevRxFilter, 1);
return filter;
} }

View File

@ -1438,14 +1438,8 @@ void virPCIDeviceAddressCopy(virPCIDeviceAddress *dst,
char * char *
virPCIDeviceAddressAsString(const virPCIDeviceAddress *addr) virPCIDeviceAddressAsString(const virPCIDeviceAddress *addr)
{ {
char *str; return g_strdup_printf(VIR_PCI_DEVICE_ADDRESS_FMT, addr->domain,
addr->bus, addr->slot, addr->function);
str = g_strdup_printf(VIR_PCI_DEVICE_ADDRESS_FMT,
addr->domain,
addr->bus,
addr->slot,
addr->function);
return str;
} }
bool bool

View File

@ -2699,16 +2699,15 @@ static int vboxDomainDestroy(virDomainPtr dom)
return vboxDomainDestroyFlags(dom, 0); return vboxDomainDestroyFlags(dom, 0);
} }
static char *vboxDomainGetOSType(virDomainPtr dom G_GNUC_UNUSED) { static char *vboxDomainGetOSType(virDomainPtr dom G_GNUC_UNUSED)
{
/* Returning "hvm" always as suggested on list, cause /* Returning "hvm" always as suggested on list, cause
* this functions seems to be badly named and it * this functions seems to be badly named and it
* is supposed to pass the ABI name and not the domain * is supposed to pass the ABI name and not the domain
* operating system driver as I had imagined ;) * operating system driver as I had imagined ;)
*/ */
char *osType;
osType = g_strdup("hvm"); return g_strdup("hvm");
return osType;
} }
static int vboxDomainSetMemory(virDomainPtr dom, unsigned long memory) static int vboxDomainSetMemory(virDomainPtr dom, unsigned long memory)

View File

@ -74,10 +74,7 @@ int virNetDevTapCreateInBridgePort(const char *brname G_GNUC_UNUSED,
char *virNetDevTapGetRealDeviceName(char *name G_GNUC_UNUSED) char *virNetDevTapGetRealDeviceName(char *name G_GNUC_UNUSED)
{ {
char *fakename; return g_strdup("faketapdev");
fakename = g_strdup("faketapdev");
return fakename;
} }
int virNetDevSetOnline(const char *ifname G_GNUC_UNUSED, int virNetDevSetOnline(const char *ifname G_GNUC_UNUSED,

View File

@ -1282,13 +1282,10 @@ qemuMonitorTestFullAddItem(qemuMonitorTest *test,
size_t line) size_t line)
{ {
g_autofree char *cmderr = NULL; g_autofree char *cmderr = NULL;
int ret;
cmderr = g_strdup_printf("wrong expected command in %s:%zu: ", filename, line); cmderr = g_strdup_printf("wrong expected command in %s:%zu: ", filename, line);
ret = qemuMonitorTestAddItemVerbatim(test, command, cmderr, response); return qemuMonitorTestAddItemVerbatim(test, command, cmderr, response);
return ret;
} }

View File

@ -128,11 +128,7 @@ static char *
get_key(const char *path, get_key(const char *path,
const char *name) const char *name)
{ {
char *ret; return g_strdup_printf("%s:%s", path, name);
ret = g_strdup_printf("%s:%s", path, name);
return ret;
} }

View File

@ -107,12 +107,9 @@ virNumaNodesetIsAvailable(virBitmap *nodeset)
char * char *
virTPMCreateCancelPath(const char *devpath) virTPMCreateCancelPath(const char *devpath)
{ {
char *path;
(void)devpath; (void)devpath;
path = g_strdup("/sys/class/misc/tpm0/device/cancel"); return g_strdup("/sys/class/misc/tpm0/device/cancel");
return path;
} }
/** /**

View File

@ -30,11 +30,7 @@ static void *
testClientNew(virNetServerClient *client G_GNUC_UNUSED, testClientNew(virNetServerClient *client G_GNUC_UNUSED,
void *opaque G_GNUC_UNUSED) void *opaque G_GNUC_UNUSED)
{ {
char *dummy; return g_new0(char, 1);
dummy = g_new0(char, 1);
return dummy;
} }

View File

@ -351,12 +351,8 @@ remove_fd(int fd)
static char * static char *
pci_address_format(struct pciDeviceAddress const *addr) pci_address_format(struct pciDeviceAddress const *addr)
{ {
char *ret; return g_strdup_printf(ADDR_STR_FMT, addr->domain, addr->bus,
addr->device, addr->function);
ret = g_strdup_printf(ADDR_STR_FMT,
addr->domain, addr->bus,
addr->device, addr->function);
return ret;
} }
static int static int

View File

@ -64,14 +64,12 @@ static char *get_fake_path(const char *real_path)
DIR *opendir(const char *name) DIR *opendir(const char *name)
{ {
g_autofree char *path = NULL; g_autofree char *path = NULL;
DIR* ret;
init_syms(); init_syms();
path = get_fake_path(name); path = get_fake_path(name);
ret = realopendir(path); return realopendir(path);
return ret;
} }
int open(const char *pathname, int flags, ...) int open(const char *pathname, int flags, ...)