SAN FRANCISCO — A judge on Friday dismissed an effort by Walgreens to undo a San Francisco law that bans tobacco sales in local drug stores.

San Francisco Superior Court Judge Peter Busch granted the city’s demurrer to Walgreens’ suit. Deputy City Attorney Vince Chhabria had argued that the city ordinance enjoyed the presumption of validity because it was “rationally related to a legitimate governmental purpose”: prohibiting health-promoting stores from selling harmful products.