From 8b5d30d4396ba0e3e0b2ff2e515d8eddb5c26849 Mon Sep 17 00:00:00 2001 From: Marek Kasik Date: Mon, 14 Jul 2014 17:02:13 +0200 Subject: [PATCH] Remove redundant checks for NULL Remove checks for NULL before g_free() and g_clear_object(). Merge check for NULL, freeing of pointer and its setting to NULL by g_clear_pointer(). https://bugzilla.gnome.org/show_bug.cgi?id=733157 --- gdk/broadway/broadway-server.c | 6 ++---- gdk/broadway/gdkwindow-broadway.c | 7 +------ gdk/gdkdevice.c | 6 ++---- gdk/quartz/GdkQuartzView.c | 15 +++------------ gdk/quartz/gdkeventloop-quartz.c | 3 +-- gdk/quartz/gdkscreen-quartz.c | 7 +------ gdk/x11/gdkwindow-x11.c | 10 ++-------- gtk/deprecated/gtknumerableicon.c | 6 +----- gtk/deprecated/gtkthemingengine.c | 3 +-- gtk/gtkaccellabel.c | 12 ++---------- gtk/gtkbutton.c | 6 +----- gtk/gtkentry.c | 6 ++---- gtk/gtkmenu.c | 18 ++++-------------- gtk/gtkpagesetupunixdialog.c | 6 +----- .../printbackends/cups/gtkprintbackendcups.c | 3 +-- testsuite/a11y/value.c | 3 +-- testsuite/gtk/filechooser.c | 3 +-- testsuite/gtk/filtermodel.c | 3 +-- 18 files changed, 28 insertions(+), 95 deletions(-) diff --git a/gdk/broadway/broadway-server.c b/gdk/broadway/broadway-server.c index 9f06c085a6..8ee4b59d38 100644 --- a/gdk/broadway/broadway-server.c +++ b/gdk/broadway/broadway-server.c @@ -1387,8 +1387,7 @@ broadway_server_destroy_window (BroadwayServer *server, g_hash_table_remove (server->id_ht, GINT_TO_POINTER (id)); - if (window->cached_surface_name != NULL) - g_free (window->cached_surface_name); + g_free (window->cached_surface_name); if (window->cached_surface != NULL) cairo_surface_destroy (window->cached_surface); @@ -1748,8 +1747,7 @@ broadway_server_open_surface (BroadwayServer *server, cairo_surface_set_user_data (surface, &shm_cairo_key, data, shm_data_unmap); - if (window->cached_surface_name != NULL) - g_free (window->cached_surface_name); + g_free (window->cached_surface_name); window->cached_surface_name = g_strdup (name); if (window->cached_surface != NULL) diff --git a/gdk/broadway/gdkwindow-broadway.c b/gdk/broadway/gdkwindow-broadway.c index cc897c5ecc..a0c19ee103 100644 --- a/gdk/broadway/gdkwindow-broadway.c +++ b/gdk/broadway/gdkwindow-broadway.c @@ -1129,12 +1129,7 @@ finish_drag (MoveResizeData *mv_resize) mv_resize->moveresize_emulation_window = NULL; g_object_unref (mv_resize->moveresize_window); mv_resize->moveresize_window = NULL; - - if (mv_resize->moveresize_pending_event) - { - g_free (mv_resize->moveresize_pending_event); - mv_resize->moveresize_pending_event = NULL; - } + g_clear_pointer (&mv_resize->moveresize_pending_event, g_free); } static gboolean diff --git a/gdk/gdkdevice.c b/gdk/gdkdevice.c index e784dfa24d..f30ebcc5bd 100644 --- a/gdk/gdkdevice.c +++ b/gdk/gdkdevice.c @@ -313,8 +313,7 @@ gdk_device_set_property (GObject *object, device->manager = g_value_get_object (value); break; case PROP_NAME: - if (device->name) - g_free (device->name); + g_free (device->name); device->name = g_value_dup_string (value); break; @@ -1477,8 +1476,7 @@ void _gdk_device_set_keys (GdkDevice *device, guint num_keys) { - if (device->keys) - g_free (device->keys); + g_free (device->keys); device->num_keys = num_keys; device->keys = g_new0 (GdkDeviceKey, num_keys); diff --git a/gdk/quartz/GdkQuartzView.c b/gdk/quartz/GdkQuartzView.c index a2ed1f58d1..cbd7fe5cbb 100644 --- a/gdk/quartz/GdkQuartzView.c +++ b/gdk/quartz/GdkQuartzView.c @@ -125,10 +125,7 @@ gchar *prev_str; markedRange = selectedRange = NSMakeRange (NSNotFound, 0); - prev_str = g_object_get_data (G_OBJECT (gdk_window), TIC_MARKED_TEXT); - if (prev_str) - g_free (prev_str); - g_object_set_data (G_OBJECT (gdk_window), TIC_MARKED_TEXT, NULL); + g_object_set_data_full (G_OBJECT (gdk_window), TIC_MARKED_TEXT, NULL, g_free); } -(void)setMarkedText: (id)aString selectedRange: (NSRange)newSelection replacementRange: (NSRange)replacementRange @@ -155,10 +152,7 @@ str = [aString UTF8String]; } - prev_str = g_object_get_data (G_OBJECT (gdk_window), TIC_MARKED_TEXT); - if (prev_str) - g_free (prev_str); - g_object_set_data (G_OBJECT (gdk_window), TIC_MARKED_TEXT, g_strdup (str)); + g_object_set_data_full (G_OBJECT (gdk_window), TIC_MARKED_TEXT, g_strdup (str), g_free); g_object_set_data (G_OBJECT (gdk_window), TIC_SELECTED_POS, GUINT_TO_POINTER (selectedRange.location)); g_object_set_data (G_OBJECT (gdk_window), TIC_SELECTED_LEN, @@ -214,10 +208,7 @@ str = [string UTF8String]; } - prev_str = g_object_get_data (G_OBJECT (gdk_window), TIC_INSERT_TEXT); - if (prev_str) - g_free (prev_str); - g_object_set_data (G_OBJECT (gdk_window), TIC_INSERT_TEXT, g_strdup (str)); + g_object_set_data_full (G_OBJECT (gdk_window), TIC_INSERT_TEXT, g_strdup (str), g_free); GDK_NOTE (EVENTS, g_print ("insertText: set %s (%p, nsview %p): %s\n", TIC_INSERT_TEXT, gdk_window, self, str ? str : "(empty)")); diff --git a/gdk/quartz/gdkeventloop-quartz.c b/gdk/quartz/gdkeventloop-quartz.c index 89d9e1ee36..c1e66b8ab7 100644 --- a/gdk/quartz/gdkeventloop-quartz.c +++ b/gdk/quartz/gdkeventloop-quartz.c @@ -245,8 +245,7 @@ select_thread_func (void *arg) * * => POLLING_DESCRIPTORS: select thread picks up the file descriptors to begin polling */ - if (current_pollfds) - g_free (current_pollfds); + g_free (current_pollfds); current_pollfds = next_pollfds; current_n_pollfds = next_n_pollfds; diff --git a/gdk/quartz/gdkscreen-quartz.c b/gdk/quartz/gdkscreen-quartz.c index ca655a5a6a..6f5762711b 100644 --- a/gdk/quartz/gdkscreen-quartz.c +++ b/gdk/quartz/gdkscreen-quartz.c @@ -107,12 +107,7 @@ static void gdk_quartz_screen_screen_rects_free (GdkQuartzScreen *screen) { screen->n_screens = 0; - - if (screen->screen_rects) - { - g_free (screen->screen_rects); - screen->screen_rects = NULL; - } + g_clear_pointer (&screen->screen_rects, g_free); } static void diff --git a/gdk/x11/gdkwindow-x11.c b/gdk/x11/gdkwindow-x11.c index fa40f0d46a..fd1ea5d047 100644 --- a/gdk/x11/gdkwindow-x11.c +++ b/gdk/x11/gdkwindow-x11.c @@ -4977,12 +4977,7 @@ finish_drag (MoveResizeData *mv_resize) mv_resize->moveresize_emulation_window = NULL; g_object_unref (mv_resize->moveresize_window); mv_resize->moveresize_window = NULL; - - if (mv_resize->moveresize_pending_event) - { - g_free (mv_resize->moveresize_pending_event); - mv_resize->moveresize_pending_event = NULL; - } + g_clear_pointer (&mv_resize->moveresize_pending_event, g_free); } static int @@ -5677,8 +5672,7 @@ gdk_x11_window_set_opaque_region (GdkWindow *window, XA_CARDINAL, 32, PropModeReplace, (guchar *) data, nitems); - if (data != NULL) - g_free (data); + g_free (data); } static gboolean diff --git a/gtk/deprecated/gtknumerableicon.c b/gtk/deprecated/gtknumerableicon.c index 6316256fe4..832390cf3c 100644 --- a/gtk/deprecated/gtknumerableicon.c +++ b/gtk/deprecated/gtknumerableicon.c @@ -902,11 +902,7 @@ gtk_numerable_icon_set_background_gicon (GtkNumerableIcon *self, g_return_if_fail (GTK_IS_NUMERABLE_ICON (self)); - if (self->priv->background_icon_name != NULL) - { - g_free (self->priv->background_icon_name); - self->priv->background_icon_name = NULL; - } + g_clear_pointer (&self->priv->background_icon_name, g_free); res = real_set_background_icon (self, icon); diff --git a/gtk/deprecated/gtkthemingengine.c b/gtk/deprecated/gtkthemingengine.c index 9248b907d4..988dd409aa 100644 --- a/gtk/deprecated/gtkthemingengine.c +++ b/gtk/deprecated/gtkthemingengine.c @@ -296,8 +296,7 @@ gtk_theming_engine_impl_set_property (GObject *object, switch (prop_id) { case PROP_NAME: - if (priv->name) - g_free (priv->name); + g_free (priv->name); priv->name = g_value_dup_string (value); break; diff --git a/gtk/gtkaccellabel.c b/gtk/gtkaccellabel.c index ba7c9ab6d0..132c1d6fcd 100644 --- a/gtk/gtkaccellabel.c +++ b/gtk/gtkaccellabel.c @@ -531,11 +531,7 @@ gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label, static void gtk_accel_label_reset (GtkAccelLabel *accel_label) { - if (accel_label->priv->accel_string) - { - g_free (accel_label->priv->accel_string); - accel_label->priv->accel_string = NULL; - } + g_clear_pointer (&accel_label->priv->accel_string, g_free); gtk_widget_queue_resize (GTK_WIDGET (accel_label)); } @@ -893,11 +889,7 @@ gtk_accel_label_refetch (GtkAccelLabel *accel_label) g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE); - if (accel_label->priv->accel_string) - { - g_free (accel_label->priv->accel_string); - accel_label->priv->accel_string = NULL; - } + g_clear_pointer (&accel_label->priv->accel_string, g_free); g_object_get (gtk_widget_get_settings (GTK_WIDGET (accel_label)), "gtk-enable-accels", &enable_accels, diff --git a/gtk/gtkbutton.c b/gtk/gtkbutton.c index 3795e7cf4d..9e0ca78ed1 100644 --- a/gtk/gtkbutton.c +++ b/gtk/gtkbutton.c @@ -692,11 +692,7 @@ gtk_button_destroy (GtkWidget *widget) GtkButton *button = GTK_BUTTON (widget); GtkButtonPrivate *priv = button->priv; - if (priv->label_text) - { - g_free (priv->label_text); - priv->label_text = NULL; - } + g_clear_pointer (&priv->label_text, g_free); g_clear_object (&priv->gesture); diff --git a/gtk/gtkentry.c b/gtk/gtkentry.c index a6adf9ff25..0c10e747bd 100644 --- a/gtk/gtkentry.c +++ b/gtk/gtkentry.c @@ -9280,8 +9280,7 @@ gtk_entry_set_icon_tooltip_text (GtkEntry *entry, if ((icon_info = priv->icons[icon_pos]) == NULL) icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos); - if (icon_info->tooltip) - g_free (icon_info->tooltip); + g_free (icon_info->tooltip); /* Treat an empty string as a NULL string, * because an empty string would be useless for a tooltip: @@ -9363,8 +9362,7 @@ gtk_entry_set_icon_tooltip_markup (GtkEntry *entry, if ((icon_info = priv->icons[icon_pos]) == NULL) icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos); - if (icon_info->tooltip) - g_free (icon_info->tooltip); + g_free (icon_info->tooltip); /* Treat an empty string as a NULL string, * because an empty string would be useless for a tooltip: diff --git a/gtk/gtkmenu.c b/gtk/gtkmenu.c index 9a21b551d2..2991136f18 100644 --- a/gtk/gtkmenu.c +++ b/gtk/gtkmenu.c @@ -1133,8 +1133,7 @@ gtk_menu_destroy (GtkWidget *widget) gtk_menu_stop_navigating_submenu (menu); - if (priv->old_active_menu_item) - g_clear_object (&priv->old_active_menu_item); + g_clear_object (&priv->old_active_menu_item); /* Add back the reference count for being a child */ if (priv->needs_destruction_ref) @@ -1143,8 +1142,7 @@ gtk_menu_destroy (GtkWidget *widget) g_object_ref (widget); } - if (priv->accel_group) - g_clear_object (&priv->accel_group); + g_clear_object (&priv->accel_group); if (priv->toplevel) gtk_widget_destroy (priv->toplevel); @@ -1152,17 +1150,9 @@ gtk_menu_destroy (GtkWidget *widget) if (priv->tearoff_window) gtk_widget_destroy (priv->tearoff_window); - if (priv->heights) - { - g_free (priv->heights); - priv->heights = NULL; - } + g_clear_pointer (&priv->heights, g_free); - if (priv->title) - { - g_free (priv->title); - priv->title = NULL; - } + g_clear_pointer (&priv->title, g_free); if (priv->position_func_data_destroy) { diff --git a/gtk/gtkpagesetupunixdialog.c b/gtk/gtkpagesetupunixdialog.c index d24d34af69..5659e47c40 100644 --- a/gtk/gtkpagesetupunixdialog.c +++ b/gtk/gtkpagesetupunixdialog.c @@ -661,11 +661,7 @@ printer_changed_callback (GtkComboBox *combo_box, /* If we're waiting for a specific printer but the user changed * to another printer, cancel that wait. */ - if (priv->waiting_for_printer) - { - g_free (priv->waiting_for_printer); - priv->waiting_for_printer = NULL; - } + g_clear_pointer (&priv->waiting_for_printer, g_free); if (priv->request_details_tag) { diff --git a/modules/printbackends/cups/gtkprintbackendcups.c b/modules/printbackends/cups/gtkprintbackendcups.c index 32ddee84ac..b3ef390392 100644 --- a/modules/printbackends/cups/gtkprintbackendcups.c +++ b/modules/printbackends/cups/gtkprintbackendcups.c @@ -3781,8 +3781,7 @@ cups_parse_user_default_printer (const char *filename, *lineptr = '\0'; - if (*printer_name != NULL) - g_free (*printer_name); + g_free (*printer_name); *printer_name = g_strdup (defname); } diff --git a/testsuite/a11y/value.c b/testsuite/a11y/value.c index 909c8e8d3e..176fd2d440 100644 --- a/testsuite/a11y/value.c +++ b/testsuite/a11y/value.c @@ -51,8 +51,7 @@ notify_cb (GObject *obj, GParamSpec *pspec, NotifyData *data) { data->count++; - if (data->last_name) - g_free (data->last_name); + g_free (data->last_name); data->last_name = g_strdup (pspec->name); } diff --git a/testsuite/gtk/filechooser.c b/testsuite/gtk/filechooser.c index 2584420e0b..5846bac0ce 100644 --- a/testsuite/gtk/filechooser.c +++ b/testsuite/gtk/filechooser.c @@ -170,8 +170,7 @@ compare_filename_cb (GtkFileChooser *chooser, gpointer data) g_assert_cmpstr (out_filename, ==, filename); - if (out_filename) - g_free (out_filename); + g_free (out_filename); } typedef struct diff --git a/testsuite/gtk/filtermodel.c b/testsuite/gtk/filtermodel.c index dcacf8d587..a67151bd2b 100644 --- a/testsuite/gtk/filtermodel.c +++ b/testsuite/gtk/filtermodel.c @@ -6205,8 +6205,7 @@ specific_bug_621076_visible_func (GtkTreeModel *model, } } - if (str) - g_free (str); + g_free (str); return visible; }