My two cents as a psychiatrist and a firearms enthusiast: I think it is fine to ask if it is pertinent to your care of the patient. In psychiatry it is absolutely pertinent, it affects suicide risk. If you're a surgeon doing an appendectomy, it's maybe not so pertinent. However, as many others have said, please approach in a non-judgmental and practical way.
Let's take the motorcycle analogy. Motorcycles are dangerous, and your life would be safer if you did not ride a motorcycle. However, no doctor would ever say, "the safest way to get around is to not ride a motorcycle", as true as that may be. No, we ask if they are wearing their helmet and practising motorcycle safety. We advise against drinking and riding, and advise not driving or riding if a person had dementia or epilepsy. Thus the discussion should be, "Oh you own guns. If you have children, we recommend they be locked. Are you practising the 3 cardinal rules of gun safety? You're having suicidal thoughts? May I advise you give your guns to your family member for safe-keeping?" This is very different than, "Did you know you are much more likely to die from a gun if you own a gun? You should really consider getting rid of your guns". This btw is a specious and faulty, meaningless tautology because you're more likely to be killed by ANYTHING if you own that thing. Indeed, if you are much more likely to die from a car accident if you have a drivers license. Well, duh.
Many in the firearms community (of which I am part of) are deeply distrustful of doctors' qualifications in discussing gun safety, as these "gun people" will likely know way more about guns and gun safety than the typical doctor. In their words, "I don't expect my gunsmith to tell me how to manage my blood pressure, and I don't expect my doctor to lecture me about proper gun safety". Most doctors have never held a gun before and don't know anything about proper gun safety. FYI, the 3 cardinal rules are: (1.) trigger discipline; finger off the trigger until read to fire, 2.) keep firearm pointed in safe direction at all times, 3.) assume the firearm is loaded at all times). There is some truth to this distrust. If you're a doctor and you want to advise people about gun safety and not sound like an idiot, may I suggest you take a gun safety course and learn how to safely handle a firearm so that you know what you're talking about when you instruct your patients?
Finally I want to make a couple more general points not necessarily related to guns and medicine/psychiatry. I live in California, a liberal "gun control" state where it is extremely difficult to (legally) own many types of guns. Criminals don't obey laws. We have a 10-round magazine limit. I've never heard of a gang-member saying to himself, "Oh man, California like, banned these high-capacity magazines, I better get rid of them!" Using gun control laws to deter violence is like trying to reduce drunk driving by making it more difficult for sober people to drive. Places with more poverty have more crime, regardless of gun control laws. Gun control laws are extremely strict in all of Latin America, yet they have extremely high levels of gun violence. One last word about guns and suicide: guns do make it easier to kill yourself, but consider that South Korea and Japan have almost no culture of civilian gun owernship and super strict gun laws, yet they have some of the highest rates of suicide in the world. People find a way to kill themselves.
Okay that's my thoughts on this topic. Peace out.