UPDATE June 2016
Bear in mind that XLIST
in Gmail IMAP was deprecated in 2013 and will eventually be removed.
You will need to use the SPECIAL-USE mailboxes instead.
Unfortunately, Gmail doesn't advertise this capability as per the spec, but it is implemented and works correctly (tested on 09/09/2016) - note the \Trash flag:
C: 55535a988a074191 LIST "" * RETURN (SPECIAL-USE)
S: ...
S: * LIST (\HasNoChildren \Trash) "/" "[Gmail]/Bin"
Regular LIST returns the same response as well (I'd say this is Gmail's bug).
Original Answer
There is a better way: XLIST command.
Google and Apple developed a special IMAP command XLIST to address this issue.
IMAP XLIST command returns a list of folders and their well-know flags (\Inbox, \Drafts, \Trash, \Sent, \Spam):
* XLIST (\HasNoChildren \Inbox) "/" "Inbox"
Mail.dll IMAP client (Commercial product I've created) supports XLIST command. It is used automatically when server advertises support for this feature. You can read more here: https://www.limilabs.com/blog/localized-gmail-imap-folders/
[Gmail]
folder is called[Google Mail]
instead. – Eyebright