Computes a unique slug for the post, when given the desired slug and some post details.

Signature

wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent )
slug
 (string) The desired slug (post_name).
post_ID
 (int) Post ID.
post_status
 (string) No uniqueness checks are made if the post is still draft or pending.
post_type
 (string) Post type.
post_parent
 (int) Post parent ID.

Return

(string) Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)

Source

function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) {
	if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) )
		return $slug;

	global $wpdb, $wp_rewrite;

	$original_slug = $slug;

	$feeds = $wp_rewrite->feeds;
	if ( ! is_array( $feeds ) )
		$feeds = array();

	$hierarchical_post_types = get_post_types( array('hierarchical' => true) );
	if ( 'attachment' == $post_type ) {
		// Attachment slugs must be unique across all types.
		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
		$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );

		/**
		 * Filter whether the post slug would make a bad attachment slug.
84 more lines...
WP Trac GitHub

Link here